xref: /openbmc/linux/kernel/time/posix-timers.c (revision 2456e855)
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>
385cee9645SThomas Gleixner 
397c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
405cee9645SThomas Gleixner #include <linux/list.h>
415cee9645SThomas Gleixner #include <linux/init.h>
425cee9645SThomas Gleixner #include <linux/compiler.h>
435cee9645SThomas Gleixner #include <linux/hash.h>
445cee9645SThomas Gleixner #include <linux/posix-clock.h>
455cee9645SThomas Gleixner #include <linux/posix-timers.h>
465cee9645SThomas Gleixner #include <linux/syscalls.h>
475cee9645SThomas Gleixner #include <linux/wait.h>
485cee9645SThomas Gleixner #include <linux/workqueue.h>
495cee9645SThomas Gleixner #include <linux/export.h>
505cee9645SThomas Gleixner #include <linux/hashtable.h>
515cee9645SThomas Gleixner 
528b094cd0SThomas Gleixner #include "timekeeping.h"
538b094cd0SThomas Gleixner 
545cee9645SThomas Gleixner /*
555cee9645SThomas Gleixner  * Management arrays for POSIX timers. Timers are now kept in static hash table
565cee9645SThomas Gleixner  * with 512 entries.
575cee9645SThomas Gleixner  * Timer ids are allocated by local routine, which selects proper hash head by
585cee9645SThomas Gleixner  * key, constructed from current->signal address and per signal struct counter.
595cee9645SThomas Gleixner  * This keeps timer ids unique per process, but now they can intersect between
605cee9645SThomas Gleixner  * processes.
615cee9645SThomas Gleixner  */
625cee9645SThomas Gleixner 
635cee9645SThomas Gleixner /*
645cee9645SThomas Gleixner  * Lets keep our timers in a slab cache :-)
655cee9645SThomas Gleixner  */
665cee9645SThomas Gleixner static struct kmem_cache *posix_timers_cache;
675cee9645SThomas Gleixner 
685cee9645SThomas Gleixner static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
695cee9645SThomas Gleixner static DEFINE_SPINLOCK(hash_lock);
705cee9645SThomas Gleixner 
715cee9645SThomas Gleixner /*
725cee9645SThomas Gleixner  * we assume that the new SIGEV_THREAD_ID shares no bits with the other
735cee9645SThomas Gleixner  * SIGEV values.  Here we put out an error if this assumption fails.
745cee9645SThomas Gleixner  */
755cee9645SThomas Gleixner #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
765cee9645SThomas Gleixner                        ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
775cee9645SThomas Gleixner #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
785cee9645SThomas Gleixner #endif
795cee9645SThomas Gleixner 
805cee9645SThomas Gleixner /*
815cee9645SThomas Gleixner  * parisc wants ENOTSUP instead of EOPNOTSUPP
825cee9645SThomas Gleixner  */
835cee9645SThomas Gleixner #ifndef ENOTSUP
845cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP EOPNOTSUPP
855cee9645SThomas Gleixner #else
865cee9645SThomas Gleixner # define ENANOSLEEP_NOTSUP ENOTSUP
875cee9645SThomas Gleixner #endif
885cee9645SThomas Gleixner 
895cee9645SThomas Gleixner /*
905cee9645SThomas Gleixner  * The timer ID is turned into a timer address by idr_find().
915cee9645SThomas Gleixner  * Verifying a valid ID consists of:
925cee9645SThomas Gleixner  *
935cee9645SThomas Gleixner  * a) checking that idr_find() returns other than -1.
945cee9645SThomas Gleixner  * b) checking that the timer id matches the one in the timer itself.
955cee9645SThomas Gleixner  * c) that the timer owner is in the callers thread group.
965cee9645SThomas Gleixner  */
975cee9645SThomas Gleixner 
985cee9645SThomas Gleixner /*
995cee9645SThomas Gleixner  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
1005cee9645SThomas Gleixner  *	    to implement others.  This structure defines the various
1015cee9645SThomas Gleixner  *	    clocks.
1025cee9645SThomas Gleixner  *
1035cee9645SThomas Gleixner  * RESOLUTION: Clock resolution is used to round up timer and interval
1045cee9645SThomas Gleixner  *	    times, NOT to report clock times, which are reported with as
1055cee9645SThomas Gleixner  *	    much resolution as the system can muster.  In some cases this
1065cee9645SThomas Gleixner  *	    resolution may depend on the underlying clock hardware and
1075cee9645SThomas Gleixner  *	    may not be quantifiable until run time, and only then is the
1085cee9645SThomas Gleixner  *	    necessary code is written.	The standard says we should say
1095cee9645SThomas Gleixner  *	    something about this issue in the documentation...
1105cee9645SThomas Gleixner  *
1115cee9645SThomas Gleixner  * FUNCTIONS: The CLOCKs structure defines possible functions to
1125cee9645SThomas Gleixner  *	    handle various clock functions.
1135cee9645SThomas Gleixner  *
1145cee9645SThomas Gleixner  *	    The standard POSIX timer management code assumes the
1155cee9645SThomas Gleixner  *	    following: 1.) The k_itimer struct (sched.h) is used for
1165cee9645SThomas Gleixner  *	    the timer.  2.) The list, it_lock, it_clock, it_id and
1175cee9645SThomas Gleixner  *	    it_pid fields are not modified by timer code.
1185cee9645SThomas Gleixner  *
1195cee9645SThomas Gleixner  * Permissions: It is assumed that the clock_settime() function defined
1205cee9645SThomas Gleixner  *	    for each clock will take care of permission checks.	 Some
1215cee9645SThomas Gleixner  *	    clocks may be set able by any user (i.e. local process
1225cee9645SThomas Gleixner  *	    clocks) others not.	 Currently the only set able clock we
1235cee9645SThomas Gleixner  *	    have is CLOCK_REALTIME and its high res counter part, both of
1245cee9645SThomas Gleixner  *	    which we beg off on and pass to do_sys_settimeofday().
1255cee9645SThomas Gleixner  */
1265cee9645SThomas Gleixner 
1275cee9645SThomas Gleixner static struct k_clock posix_clocks[MAX_CLOCKS];
1285cee9645SThomas Gleixner 
1295cee9645SThomas Gleixner /*
1305cee9645SThomas Gleixner  * These ones are defined below.
1315cee9645SThomas Gleixner  */
1325cee9645SThomas Gleixner static int common_nsleep(const clockid_t, int flags, struct timespec *t,
1335cee9645SThomas Gleixner 			 struct timespec __user *rmtp);
1345cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer);
1355cee9645SThomas Gleixner static void common_timer_get(struct k_itimer *, struct itimerspec *);
1365cee9645SThomas Gleixner static int common_timer_set(struct k_itimer *, int,
1375cee9645SThomas Gleixner 			    struct itimerspec *, struct itimerspec *);
1385cee9645SThomas Gleixner static int common_timer_del(struct k_itimer *timer);
1395cee9645SThomas Gleixner 
1405cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
1415cee9645SThomas Gleixner 
1425cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
1435cee9645SThomas Gleixner 
1445cee9645SThomas Gleixner #define lock_timer(tid, flags)						   \
1455cee9645SThomas Gleixner ({	struct k_itimer *__timr;					   \
1465cee9645SThomas Gleixner 	__cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags));  \
1475cee9645SThomas Gleixner 	__timr;								   \
1485cee9645SThomas Gleixner })
1495cee9645SThomas Gleixner 
1505cee9645SThomas Gleixner static int hash(struct signal_struct *sig, unsigned int nr)
1515cee9645SThomas Gleixner {
1525cee9645SThomas Gleixner 	return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
1535cee9645SThomas Gleixner }
1545cee9645SThomas Gleixner 
1555cee9645SThomas Gleixner static struct k_itimer *__posix_timers_find(struct hlist_head *head,
1565cee9645SThomas Gleixner 					    struct signal_struct *sig,
1575cee9645SThomas Gleixner 					    timer_t id)
1585cee9645SThomas Gleixner {
1595cee9645SThomas Gleixner 	struct k_itimer *timer;
1605cee9645SThomas Gleixner 
1615cee9645SThomas Gleixner 	hlist_for_each_entry_rcu(timer, head, t_hash) {
1625cee9645SThomas Gleixner 		if ((timer->it_signal == sig) && (timer->it_id == id))
1635cee9645SThomas Gleixner 			return timer;
1645cee9645SThomas Gleixner 	}
1655cee9645SThomas Gleixner 	return NULL;
1665cee9645SThomas Gleixner }
1675cee9645SThomas Gleixner 
1685cee9645SThomas Gleixner static struct k_itimer *posix_timer_by_id(timer_t id)
1695cee9645SThomas Gleixner {
1705cee9645SThomas Gleixner 	struct signal_struct *sig = current->signal;
1715cee9645SThomas Gleixner 	struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
1725cee9645SThomas Gleixner 
1735cee9645SThomas Gleixner 	return __posix_timers_find(head, sig, id);
1745cee9645SThomas Gleixner }
1755cee9645SThomas Gleixner 
1765cee9645SThomas Gleixner static int posix_timer_add(struct k_itimer *timer)
1775cee9645SThomas Gleixner {
1785cee9645SThomas Gleixner 	struct signal_struct *sig = current->signal;
1795cee9645SThomas Gleixner 	int first_free_id = sig->posix_timer_id;
1805cee9645SThomas Gleixner 	struct hlist_head *head;
1815cee9645SThomas Gleixner 	int ret = -ENOENT;
1825cee9645SThomas Gleixner 
1835cee9645SThomas Gleixner 	do {
1845cee9645SThomas Gleixner 		spin_lock(&hash_lock);
1855cee9645SThomas Gleixner 		head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
1865cee9645SThomas Gleixner 		if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
1875cee9645SThomas Gleixner 			hlist_add_head_rcu(&timer->t_hash, head);
1885cee9645SThomas Gleixner 			ret = sig->posix_timer_id;
1895cee9645SThomas Gleixner 		}
1905cee9645SThomas Gleixner 		if (++sig->posix_timer_id < 0)
1915cee9645SThomas Gleixner 			sig->posix_timer_id = 0;
1925cee9645SThomas Gleixner 		if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
1935cee9645SThomas Gleixner 			/* Loop over all possible ids completed */
1945cee9645SThomas Gleixner 			ret = -EAGAIN;
1955cee9645SThomas Gleixner 		spin_unlock(&hash_lock);
1965cee9645SThomas Gleixner 	} while (ret == -ENOENT);
1975cee9645SThomas Gleixner 	return ret;
1985cee9645SThomas Gleixner }
1995cee9645SThomas Gleixner 
2005cee9645SThomas Gleixner static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
2015cee9645SThomas Gleixner {
2025cee9645SThomas Gleixner 	spin_unlock_irqrestore(&timr->it_lock, flags);
2035cee9645SThomas Gleixner }
2045cee9645SThomas Gleixner 
2055cee9645SThomas Gleixner /* Get clock_realtime */
2065cee9645SThomas Gleixner static int posix_clock_realtime_get(clockid_t which_clock, struct timespec *tp)
2075cee9645SThomas Gleixner {
2085cee9645SThomas Gleixner 	ktime_get_real_ts(tp);
2095cee9645SThomas Gleixner 	return 0;
2105cee9645SThomas Gleixner }
2115cee9645SThomas Gleixner 
2125cee9645SThomas Gleixner /* Set clock_realtime */
2135cee9645SThomas Gleixner static int posix_clock_realtime_set(const clockid_t which_clock,
2145cee9645SThomas Gleixner 				    const struct timespec *tp)
2155cee9645SThomas Gleixner {
2165cee9645SThomas Gleixner 	return do_sys_settimeofday(tp, NULL);
2175cee9645SThomas Gleixner }
2185cee9645SThomas Gleixner 
2195cee9645SThomas Gleixner static int posix_clock_realtime_adj(const clockid_t which_clock,
2205cee9645SThomas Gleixner 				    struct timex *t)
2215cee9645SThomas Gleixner {
2225cee9645SThomas Gleixner 	return do_adjtimex(t);
2235cee9645SThomas Gleixner }
2245cee9645SThomas Gleixner 
2255cee9645SThomas Gleixner /*
2265cee9645SThomas Gleixner  * Get monotonic time for posix timers
2275cee9645SThomas Gleixner  */
2285cee9645SThomas Gleixner static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
2295cee9645SThomas Gleixner {
2305cee9645SThomas Gleixner 	ktime_get_ts(tp);
2315cee9645SThomas Gleixner 	return 0;
2325cee9645SThomas Gleixner }
2335cee9645SThomas Gleixner 
2345cee9645SThomas Gleixner /*
2355cee9645SThomas Gleixner  * Get monotonic-raw time for posix timers
2365cee9645SThomas Gleixner  */
2375cee9645SThomas Gleixner static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)
2385cee9645SThomas Gleixner {
2395cee9645SThomas Gleixner 	getrawmonotonic(tp);
2405cee9645SThomas Gleixner 	return 0;
2415cee9645SThomas Gleixner }
2425cee9645SThomas Gleixner 
2435cee9645SThomas Gleixner 
2445cee9645SThomas Gleixner static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec *tp)
2455cee9645SThomas Gleixner {
2465cee9645SThomas Gleixner 	*tp = current_kernel_time();
2475cee9645SThomas Gleixner 	return 0;
2485cee9645SThomas Gleixner }
2495cee9645SThomas Gleixner 
2505cee9645SThomas Gleixner static int posix_get_monotonic_coarse(clockid_t which_clock,
2515cee9645SThomas Gleixner 						struct timespec *tp)
2525cee9645SThomas Gleixner {
2535cee9645SThomas Gleixner 	*tp = get_monotonic_coarse();
2545cee9645SThomas Gleixner 	return 0;
2555cee9645SThomas Gleixner }
2565cee9645SThomas Gleixner 
2575cee9645SThomas Gleixner static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp)
2585cee9645SThomas Gleixner {
2595cee9645SThomas Gleixner 	*tp = ktime_to_timespec(KTIME_LOW_RES);
2605cee9645SThomas Gleixner 	return 0;
2615cee9645SThomas Gleixner }
2625cee9645SThomas Gleixner 
2635cee9645SThomas Gleixner static int posix_get_boottime(const clockid_t which_clock, struct timespec *tp)
2645cee9645SThomas Gleixner {
2655cee9645SThomas Gleixner 	get_monotonic_boottime(tp);
2665cee9645SThomas Gleixner 	return 0;
2675cee9645SThomas Gleixner }
2685cee9645SThomas Gleixner 
2695cee9645SThomas Gleixner static int posix_get_tai(clockid_t which_clock, struct timespec *tp)
2705cee9645SThomas Gleixner {
2715cee9645SThomas Gleixner 	timekeeping_clocktai(tp);
2725cee9645SThomas Gleixner 	return 0;
2735cee9645SThomas Gleixner }
2745cee9645SThomas Gleixner 
275056a3cacSThomas Gleixner static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec *tp)
276056a3cacSThomas Gleixner {
277056a3cacSThomas Gleixner 	tp->tv_sec = 0;
278056a3cacSThomas Gleixner 	tp->tv_nsec = hrtimer_resolution;
279056a3cacSThomas Gleixner 	return 0;
280056a3cacSThomas Gleixner }
281056a3cacSThomas Gleixner 
2825cee9645SThomas Gleixner /*
2835cee9645SThomas Gleixner  * Initialize everything, well, just everything in Posix clocks/timers ;)
2845cee9645SThomas Gleixner  */
2855cee9645SThomas Gleixner static __init int init_posix_timers(void)
2865cee9645SThomas Gleixner {
2875cee9645SThomas Gleixner 	struct k_clock clock_realtime = {
288056a3cacSThomas Gleixner 		.clock_getres	= posix_get_hrtimer_res,
2895cee9645SThomas Gleixner 		.clock_get	= posix_clock_realtime_get,
2905cee9645SThomas Gleixner 		.clock_set	= posix_clock_realtime_set,
2915cee9645SThomas Gleixner 		.clock_adj	= posix_clock_realtime_adj,
2925cee9645SThomas Gleixner 		.nsleep		= common_nsleep,
2935cee9645SThomas Gleixner 		.nsleep_restart	= hrtimer_nanosleep_restart,
2945cee9645SThomas Gleixner 		.timer_create	= common_timer_create,
2955cee9645SThomas Gleixner 		.timer_set	= common_timer_set,
2965cee9645SThomas Gleixner 		.timer_get	= common_timer_get,
2975cee9645SThomas Gleixner 		.timer_del	= common_timer_del,
2985cee9645SThomas Gleixner 	};
2995cee9645SThomas Gleixner 	struct k_clock clock_monotonic = {
300056a3cacSThomas Gleixner 		.clock_getres	= posix_get_hrtimer_res,
3015cee9645SThomas Gleixner 		.clock_get	= posix_ktime_get_ts,
3025cee9645SThomas Gleixner 		.nsleep		= common_nsleep,
3035cee9645SThomas Gleixner 		.nsleep_restart	= hrtimer_nanosleep_restart,
3045cee9645SThomas Gleixner 		.timer_create	= common_timer_create,
3055cee9645SThomas Gleixner 		.timer_set	= common_timer_set,
3065cee9645SThomas Gleixner 		.timer_get	= common_timer_get,
3075cee9645SThomas Gleixner 		.timer_del	= common_timer_del,
3085cee9645SThomas Gleixner 	};
3095cee9645SThomas Gleixner 	struct k_clock clock_monotonic_raw = {
310056a3cacSThomas Gleixner 		.clock_getres	= posix_get_hrtimer_res,
3115cee9645SThomas Gleixner 		.clock_get	= posix_get_monotonic_raw,
3125cee9645SThomas Gleixner 	};
3135cee9645SThomas Gleixner 	struct k_clock clock_realtime_coarse = {
3145cee9645SThomas Gleixner 		.clock_getres	= posix_get_coarse_res,
3155cee9645SThomas Gleixner 		.clock_get	= posix_get_realtime_coarse,
3165cee9645SThomas Gleixner 	};
3175cee9645SThomas Gleixner 	struct k_clock clock_monotonic_coarse = {
3185cee9645SThomas Gleixner 		.clock_getres	= posix_get_coarse_res,
3195cee9645SThomas Gleixner 		.clock_get	= posix_get_monotonic_coarse,
3205cee9645SThomas Gleixner 	};
3215cee9645SThomas Gleixner 	struct k_clock clock_tai = {
322056a3cacSThomas Gleixner 		.clock_getres	= posix_get_hrtimer_res,
3235cee9645SThomas Gleixner 		.clock_get	= posix_get_tai,
3245cee9645SThomas Gleixner 		.nsleep		= common_nsleep,
3255cee9645SThomas Gleixner 		.nsleep_restart	= hrtimer_nanosleep_restart,
3265cee9645SThomas Gleixner 		.timer_create	= common_timer_create,
3275cee9645SThomas Gleixner 		.timer_set	= common_timer_set,
3285cee9645SThomas Gleixner 		.timer_get	= common_timer_get,
3295cee9645SThomas Gleixner 		.timer_del	= common_timer_del,
3305cee9645SThomas Gleixner 	};
3315cee9645SThomas Gleixner 	struct k_clock clock_boottime = {
332056a3cacSThomas Gleixner 		.clock_getres	= posix_get_hrtimer_res,
3335cee9645SThomas Gleixner 		.clock_get	= posix_get_boottime,
3345cee9645SThomas Gleixner 		.nsleep		= common_nsleep,
3355cee9645SThomas Gleixner 		.nsleep_restart	= hrtimer_nanosleep_restart,
3365cee9645SThomas Gleixner 		.timer_create	= common_timer_create,
3375cee9645SThomas Gleixner 		.timer_set	= common_timer_set,
3385cee9645SThomas Gleixner 		.timer_get	= common_timer_get,
3395cee9645SThomas Gleixner 		.timer_del	= common_timer_del,
3405cee9645SThomas Gleixner 	};
3415cee9645SThomas Gleixner 
3425cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
3435cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
3445cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
3455cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
3465cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
3475cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
3485cee9645SThomas Gleixner 	posix_timers_register_clock(CLOCK_TAI, &clock_tai);
3495cee9645SThomas Gleixner 
3505cee9645SThomas Gleixner 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
3515cee9645SThomas Gleixner 					sizeof (struct k_itimer), 0, SLAB_PANIC,
3525cee9645SThomas Gleixner 					NULL);
3535cee9645SThomas Gleixner 	return 0;
3545cee9645SThomas Gleixner }
3555cee9645SThomas Gleixner 
3565cee9645SThomas Gleixner __initcall(init_posix_timers);
3575cee9645SThomas Gleixner 
3585cee9645SThomas Gleixner static void schedule_next_timer(struct k_itimer *timr)
3595cee9645SThomas Gleixner {
3605cee9645SThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
3615cee9645SThomas Gleixner 
3622456e855SThomas Gleixner 	if (timr->it.real.interval == 0)
3635cee9645SThomas Gleixner 		return;
3645cee9645SThomas Gleixner 
3655cee9645SThomas Gleixner 	timr->it_overrun += (unsigned int) hrtimer_forward(timer,
3665cee9645SThomas Gleixner 						timer->base->get_time(),
3675cee9645SThomas Gleixner 						timr->it.real.interval);
3685cee9645SThomas Gleixner 
3695cee9645SThomas Gleixner 	timr->it_overrun_last = timr->it_overrun;
3705cee9645SThomas Gleixner 	timr->it_overrun = -1;
3715cee9645SThomas Gleixner 	++timr->it_requeue_pending;
3725cee9645SThomas Gleixner 	hrtimer_restart(timer);
3735cee9645SThomas Gleixner }
3745cee9645SThomas Gleixner 
3755cee9645SThomas Gleixner /*
3765cee9645SThomas Gleixner  * This function is exported for use by the signal deliver code.  It is
3775cee9645SThomas Gleixner  * called just prior to the info block being released and passes that
3785cee9645SThomas Gleixner  * block to us.  It's function is to update the overrun entry AND to
3795cee9645SThomas Gleixner  * restart the timer.  It should only be called if the timer is to be
3805cee9645SThomas Gleixner  * restarted (i.e. we have flagged this in the sys_private entry of the
3815cee9645SThomas Gleixner  * info block).
3825cee9645SThomas Gleixner  *
3835cee9645SThomas Gleixner  * To protect against the timer going away while the interrupt is queued,
3845cee9645SThomas Gleixner  * we require that the it_requeue_pending flag be set.
3855cee9645SThomas Gleixner  */
3865cee9645SThomas Gleixner void do_schedule_next_timer(struct siginfo *info)
3875cee9645SThomas Gleixner {
3885cee9645SThomas Gleixner 	struct k_itimer *timr;
3895cee9645SThomas Gleixner 	unsigned long flags;
3905cee9645SThomas Gleixner 
3915cee9645SThomas Gleixner 	timr = lock_timer(info->si_tid, &flags);
3925cee9645SThomas Gleixner 
3935cee9645SThomas Gleixner 	if (timr && timr->it_requeue_pending == info->si_sys_private) {
3945cee9645SThomas Gleixner 		if (timr->it_clock < 0)
3955cee9645SThomas Gleixner 			posix_cpu_timer_schedule(timr);
3965cee9645SThomas Gleixner 		else
3975cee9645SThomas Gleixner 			schedule_next_timer(timr);
3985cee9645SThomas Gleixner 
3995cee9645SThomas Gleixner 		info->si_overrun += timr->it_overrun_last;
4005cee9645SThomas Gleixner 	}
4015cee9645SThomas Gleixner 
4025cee9645SThomas Gleixner 	if (timr)
4035cee9645SThomas Gleixner 		unlock_timer(timr, flags);
4045cee9645SThomas Gleixner }
4055cee9645SThomas Gleixner 
4065cee9645SThomas Gleixner int posix_timer_event(struct k_itimer *timr, int si_private)
4075cee9645SThomas Gleixner {
4085cee9645SThomas Gleixner 	struct task_struct *task;
4095cee9645SThomas Gleixner 	int shared, ret = -1;
4105cee9645SThomas Gleixner 	/*
4115cee9645SThomas Gleixner 	 * FIXME: if ->sigq is queued we can race with
4125cee9645SThomas Gleixner 	 * dequeue_signal()->do_schedule_next_timer().
4135cee9645SThomas Gleixner 	 *
4145cee9645SThomas Gleixner 	 * If dequeue_signal() sees the "right" value of
4155cee9645SThomas Gleixner 	 * si_sys_private it calls do_schedule_next_timer().
4165cee9645SThomas Gleixner 	 * We re-queue ->sigq and drop ->it_lock().
4175cee9645SThomas Gleixner 	 * do_schedule_next_timer() locks the timer
4185cee9645SThomas Gleixner 	 * and re-schedules it while ->sigq is pending.
4195cee9645SThomas Gleixner 	 * Not really bad, but not that we want.
4205cee9645SThomas Gleixner 	 */
4215cee9645SThomas Gleixner 	timr->sigq->info.si_sys_private = si_private;
4225cee9645SThomas Gleixner 
4235cee9645SThomas Gleixner 	rcu_read_lock();
4245cee9645SThomas Gleixner 	task = pid_task(timr->it_pid, PIDTYPE_PID);
4255cee9645SThomas Gleixner 	if (task) {
4265cee9645SThomas Gleixner 		shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
4275cee9645SThomas Gleixner 		ret = send_sigqueue(timr->sigq, task, shared);
4285cee9645SThomas Gleixner 	}
4295cee9645SThomas Gleixner 	rcu_read_unlock();
4305cee9645SThomas Gleixner 	/* If we failed to send the signal the timer stops. */
4315cee9645SThomas Gleixner 	return ret > 0;
4325cee9645SThomas Gleixner }
4335cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(posix_timer_event);
4345cee9645SThomas Gleixner 
4355cee9645SThomas Gleixner /*
4365cee9645SThomas Gleixner  * This function gets called when a POSIX.1b interval timer expires.  It
4375cee9645SThomas Gleixner  * is used as a callback from the kernel internal timer.  The
4385cee9645SThomas Gleixner  * run_timer_list code ALWAYS calls with interrupts on.
4395cee9645SThomas Gleixner 
4405cee9645SThomas Gleixner  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
4415cee9645SThomas Gleixner  */
4425cee9645SThomas Gleixner static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
4435cee9645SThomas Gleixner {
4445cee9645SThomas Gleixner 	struct k_itimer *timr;
4455cee9645SThomas Gleixner 	unsigned long flags;
4465cee9645SThomas Gleixner 	int si_private = 0;
4475cee9645SThomas Gleixner 	enum hrtimer_restart ret = HRTIMER_NORESTART;
4485cee9645SThomas Gleixner 
4495cee9645SThomas Gleixner 	timr = container_of(timer, struct k_itimer, it.real.timer);
4505cee9645SThomas Gleixner 	spin_lock_irqsave(&timr->it_lock, flags);
4515cee9645SThomas Gleixner 
4522456e855SThomas Gleixner 	if (timr->it.real.interval != 0)
4535cee9645SThomas Gleixner 		si_private = ++timr->it_requeue_pending;
4545cee9645SThomas Gleixner 
4555cee9645SThomas Gleixner 	if (posix_timer_event(timr, si_private)) {
4565cee9645SThomas Gleixner 		/*
4575cee9645SThomas Gleixner 		 * signal was not sent because of sig_ignor
4585cee9645SThomas Gleixner 		 * we will not get a call back to restart it AND
4595cee9645SThomas Gleixner 		 * it should be restarted.
4605cee9645SThomas Gleixner 		 */
4612456e855SThomas Gleixner 		if (timr->it.real.interval != 0) {
4625cee9645SThomas Gleixner 			ktime_t now = hrtimer_cb_get_time(timer);
4635cee9645SThomas Gleixner 
4645cee9645SThomas Gleixner 			/*
4655cee9645SThomas Gleixner 			 * FIXME: What we really want, is to stop this
4665cee9645SThomas Gleixner 			 * timer completely and restart it in case the
4675cee9645SThomas Gleixner 			 * SIG_IGN is removed. This is a non trivial
4685cee9645SThomas Gleixner 			 * change which involves sighand locking
4695cee9645SThomas Gleixner 			 * (sigh !), which we don't want to do late in
4705cee9645SThomas Gleixner 			 * the release cycle.
4715cee9645SThomas Gleixner 			 *
4725cee9645SThomas Gleixner 			 * For now we just let timers with an interval
4735cee9645SThomas Gleixner 			 * less than a jiffie expire every jiffie to
4745cee9645SThomas Gleixner 			 * avoid softirq starvation in case of SIG_IGN
4755cee9645SThomas Gleixner 			 * and a very small interval, which would put
4765cee9645SThomas Gleixner 			 * the timer right back on the softirq pending
4775cee9645SThomas Gleixner 			 * list. By moving now ahead of time we trick
4785cee9645SThomas Gleixner 			 * hrtimer_forward() to expire the timer
4795cee9645SThomas Gleixner 			 * later, while we still maintain the overrun
4805cee9645SThomas Gleixner 			 * accuracy, but have some inconsistency in
4815cee9645SThomas Gleixner 			 * the timer_gettime() case. This is at least
4825cee9645SThomas Gleixner 			 * better than a starved softirq. A more
4835cee9645SThomas Gleixner 			 * complex fix which solves also another related
4845cee9645SThomas Gleixner 			 * inconsistency is already in the pipeline.
4855cee9645SThomas Gleixner 			 */
4865cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
4875cee9645SThomas Gleixner 			{
4885cee9645SThomas Gleixner 				ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
4895cee9645SThomas Gleixner 
4902456e855SThomas Gleixner 				if (timr->it.real.interval < kj)
4915cee9645SThomas Gleixner 					now = ktime_add(now, kj);
4925cee9645SThomas Gleixner 			}
4935cee9645SThomas Gleixner #endif
4945cee9645SThomas Gleixner 			timr->it_overrun += (unsigned int)
4955cee9645SThomas Gleixner 				hrtimer_forward(timer, now,
4965cee9645SThomas Gleixner 						timr->it.real.interval);
4975cee9645SThomas Gleixner 			ret = HRTIMER_RESTART;
4985cee9645SThomas Gleixner 			++timr->it_requeue_pending;
4995cee9645SThomas Gleixner 		}
5005cee9645SThomas Gleixner 	}
5015cee9645SThomas Gleixner 
5025cee9645SThomas Gleixner 	unlock_timer(timr, flags);
5035cee9645SThomas Gleixner 	return ret;
5045cee9645SThomas Gleixner }
5055cee9645SThomas Gleixner 
5065cee9645SThomas Gleixner static struct pid *good_sigevent(sigevent_t * event)
5075cee9645SThomas Gleixner {
5085cee9645SThomas Gleixner 	struct task_struct *rtn = current->group_leader;
5095cee9645SThomas Gleixner 
5105cee9645SThomas Gleixner 	if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
5115cee9645SThomas Gleixner 		(!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
5125cee9645SThomas Gleixner 		 !same_thread_group(rtn, current) ||
5135cee9645SThomas Gleixner 		 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
5145cee9645SThomas Gleixner 		return NULL;
5155cee9645SThomas Gleixner 
5165cee9645SThomas Gleixner 	if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
5175cee9645SThomas Gleixner 	    ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
5185cee9645SThomas Gleixner 		return NULL;
5195cee9645SThomas Gleixner 
5205cee9645SThomas Gleixner 	return task_pid(rtn);
5215cee9645SThomas Gleixner }
5225cee9645SThomas Gleixner 
5235cee9645SThomas Gleixner void posix_timers_register_clock(const clockid_t clock_id,
5245cee9645SThomas Gleixner 				 struct k_clock *new_clock)
5255cee9645SThomas Gleixner {
5265cee9645SThomas Gleixner 	if ((unsigned) clock_id >= MAX_CLOCKS) {
5275cee9645SThomas Gleixner 		printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
5285cee9645SThomas Gleixner 		       clock_id);
5295cee9645SThomas Gleixner 		return;
5305cee9645SThomas Gleixner 	}
5315cee9645SThomas Gleixner 
5325cee9645SThomas Gleixner 	if (!new_clock->clock_get) {
5335cee9645SThomas Gleixner 		printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
5345cee9645SThomas Gleixner 		       clock_id);
5355cee9645SThomas Gleixner 		return;
5365cee9645SThomas Gleixner 	}
5375cee9645SThomas Gleixner 	if (!new_clock->clock_getres) {
5385cee9645SThomas Gleixner 		printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
5395cee9645SThomas Gleixner 		       clock_id);
5405cee9645SThomas Gleixner 		return;
5415cee9645SThomas Gleixner 	}
5425cee9645SThomas Gleixner 
5435cee9645SThomas Gleixner 	posix_clocks[clock_id] = *new_clock;
5445cee9645SThomas Gleixner }
5455cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(posix_timers_register_clock);
5465cee9645SThomas Gleixner 
5475cee9645SThomas Gleixner static struct k_itimer * alloc_posix_timer(void)
5485cee9645SThomas Gleixner {
5495cee9645SThomas Gleixner 	struct k_itimer *tmr;
5505cee9645SThomas Gleixner 	tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
5515cee9645SThomas Gleixner 	if (!tmr)
5525cee9645SThomas Gleixner 		return tmr;
5535cee9645SThomas Gleixner 	if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
5545cee9645SThomas Gleixner 		kmem_cache_free(posix_timers_cache, tmr);
5555cee9645SThomas Gleixner 		return NULL;
5565cee9645SThomas Gleixner 	}
5575cee9645SThomas Gleixner 	memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
5585cee9645SThomas Gleixner 	return tmr;
5595cee9645SThomas Gleixner }
5605cee9645SThomas Gleixner 
5615cee9645SThomas Gleixner static void k_itimer_rcu_free(struct rcu_head *head)
5625cee9645SThomas Gleixner {
5635cee9645SThomas Gleixner 	struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
5645cee9645SThomas Gleixner 
5655cee9645SThomas Gleixner 	kmem_cache_free(posix_timers_cache, tmr);
5665cee9645SThomas Gleixner }
5675cee9645SThomas Gleixner 
5685cee9645SThomas Gleixner #define IT_ID_SET	1
5695cee9645SThomas Gleixner #define IT_ID_NOT_SET	0
5705cee9645SThomas Gleixner static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
5715cee9645SThomas Gleixner {
5725cee9645SThomas Gleixner 	if (it_id_set) {
5735cee9645SThomas Gleixner 		unsigned long flags;
5745cee9645SThomas Gleixner 		spin_lock_irqsave(&hash_lock, flags);
5755cee9645SThomas Gleixner 		hlist_del_rcu(&tmr->t_hash);
5765cee9645SThomas Gleixner 		spin_unlock_irqrestore(&hash_lock, flags);
5775cee9645SThomas Gleixner 	}
5785cee9645SThomas Gleixner 	put_pid(tmr->it_pid);
5795cee9645SThomas Gleixner 	sigqueue_free(tmr->sigq);
5805cee9645SThomas Gleixner 	call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
5815cee9645SThomas Gleixner }
5825cee9645SThomas Gleixner 
5835cee9645SThomas Gleixner static struct k_clock *clockid_to_kclock(const clockid_t id)
5845cee9645SThomas Gleixner {
5855cee9645SThomas Gleixner 	if (id < 0)
5865cee9645SThomas Gleixner 		return (id & CLOCKFD_MASK) == CLOCKFD ?
5875cee9645SThomas Gleixner 			&clock_posix_dynamic : &clock_posix_cpu;
5885cee9645SThomas Gleixner 
5895cee9645SThomas Gleixner 	if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
5905cee9645SThomas Gleixner 		return NULL;
5915cee9645SThomas Gleixner 	return &posix_clocks[id];
5925cee9645SThomas Gleixner }
5935cee9645SThomas Gleixner 
5945cee9645SThomas Gleixner static int common_timer_create(struct k_itimer *new_timer)
5955cee9645SThomas Gleixner {
5965cee9645SThomas Gleixner 	hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
5975cee9645SThomas Gleixner 	return 0;
5985cee9645SThomas Gleixner }
5995cee9645SThomas Gleixner 
6005cee9645SThomas Gleixner /* Create a POSIX.1b interval timer. */
6015cee9645SThomas Gleixner 
6025cee9645SThomas Gleixner SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
6035cee9645SThomas Gleixner 		struct sigevent __user *, timer_event_spec,
6045cee9645SThomas Gleixner 		timer_t __user *, created_timer_id)
6055cee9645SThomas Gleixner {
6065cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
6075cee9645SThomas Gleixner 	struct k_itimer *new_timer;
6085cee9645SThomas Gleixner 	int error, new_timer_id;
6095cee9645SThomas Gleixner 	sigevent_t event;
6105cee9645SThomas Gleixner 	int it_id_set = IT_ID_NOT_SET;
6115cee9645SThomas Gleixner 
6125cee9645SThomas Gleixner 	if (!kc)
6135cee9645SThomas Gleixner 		return -EINVAL;
6145cee9645SThomas Gleixner 	if (!kc->timer_create)
6155cee9645SThomas Gleixner 		return -EOPNOTSUPP;
6165cee9645SThomas Gleixner 
6175cee9645SThomas Gleixner 	new_timer = alloc_posix_timer();
6185cee9645SThomas Gleixner 	if (unlikely(!new_timer))
6195cee9645SThomas Gleixner 		return -EAGAIN;
6205cee9645SThomas Gleixner 
6215cee9645SThomas Gleixner 	spin_lock_init(&new_timer->it_lock);
6225cee9645SThomas Gleixner 	new_timer_id = posix_timer_add(new_timer);
6235cee9645SThomas Gleixner 	if (new_timer_id < 0) {
6245cee9645SThomas Gleixner 		error = new_timer_id;
6255cee9645SThomas Gleixner 		goto out;
6265cee9645SThomas Gleixner 	}
6275cee9645SThomas Gleixner 
6285cee9645SThomas Gleixner 	it_id_set = IT_ID_SET;
6295cee9645SThomas Gleixner 	new_timer->it_id = (timer_t) new_timer_id;
6305cee9645SThomas Gleixner 	new_timer->it_clock = which_clock;
6315cee9645SThomas Gleixner 	new_timer->it_overrun = -1;
6325cee9645SThomas Gleixner 
6335cee9645SThomas Gleixner 	if (timer_event_spec) {
6345cee9645SThomas Gleixner 		if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
6355cee9645SThomas Gleixner 			error = -EFAULT;
6365cee9645SThomas Gleixner 			goto out;
6375cee9645SThomas Gleixner 		}
6385cee9645SThomas Gleixner 		rcu_read_lock();
6395cee9645SThomas Gleixner 		new_timer->it_pid = get_pid(good_sigevent(&event));
6405cee9645SThomas Gleixner 		rcu_read_unlock();
6415cee9645SThomas Gleixner 		if (!new_timer->it_pid) {
6425cee9645SThomas Gleixner 			error = -EINVAL;
6435cee9645SThomas Gleixner 			goto out;
6445cee9645SThomas Gleixner 		}
6455cee9645SThomas Gleixner 	} else {
6466891c450SMathias Krause 		memset(&event.sigev_value, 0, sizeof(event.sigev_value));
6475cee9645SThomas Gleixner 		event.sigev_notify = SIGEV_SIGNAL;
6485cee9645SThomas Gleixner 		event.sigev_signo = SIGALRM;
6495cee9645SThomas Gleixner 		event.sigev_value.sival_int = new_timer->it_id;
6505cee9645SThomas Gleixner 		new_timer->it_pid = get_pid(task_tgid(current));
6515cee9645SThomas Gleixner 	}
6525cee9645SThomas Gleixner 
6535cee9645SThomas Gleixner 	new_timer->it_sigev_notify     = event.sigev_notify;
6545cee9645SThomas Gleixner 	new_timer->sigq->info.si_signo = event.sigev_signo;
6555cee9645SThomas Gleixner 	new_timer->sigq->info.si_value = event.sigev_value;
6565cee9645SThomas Gleixner 	new_timer->sigq->info.si_tid   = new_timer->it_id;
6575cee9645SThomas Gleixner 	new_timer->sigq->info.si_code  = SI_TIMER;
6585cee9645SThomas Gleixner 
6595cee9645SThomas Gleixner 	if (copy_to_user(created_timer_id,
6605cee9645SThomas Gleixner 			 &new_timer_id, sizeof (new_timer_id))) {
6615cee9645SThomas Gleixner 		error = -EFAULT;
6625cee9645SThomas Gleixner 		goto out;
6635cee9645SThomas Gleixner 	}
6645cee9645SThomas Gleixner 
6655cee9645SThomas Gleixner 	error = kc->timer_create(new_timer);
6665cee9645SThomas Gleixner 	if (error)
6675cee9645SThomas Gleixner 		goto out;
6685cee9645SThomas Gleixner 
6695cee9645SThomas Gleixner 	spin_lock_irq(&current->sighand->siglock);
6705cee9645SThomas Gleixner 	new_timer->it_signal = current->signal;
6715cee9645SThomas Gleixner 	list_add(&new_timer->list, &current->signal->posix_timers);
6725cee9645SThomas Gleixner 	spin_unlock_irq(&current->sighand->siglock);
6735cee9645SThomas Gleixner 
6745cee9645SThomas Gleixner 	return 0;
6755cee9645SThomas Gleixner 	/*
6765cee9645SThomas Gleixner 	 * In the case of the timer belonging to another task, after
6775cee9645SThomas Gleixner 	 * the task is unlocked, the timer is owned by the other task
6785cee9645SThomas Gleixner 	 * and may cease to exist at any time.  Don't use or modify
6795cee9645SThomas Gleixner 	 * new_timer after the unlock call.
6805cee9645SThomas Gleixner 	 */
6815cee9645SThomas Gleixner out:
6825cee9645SThomas Gleixner 	release_posix_timer(new_timer, it_id_set);
6835cee9645SThomas Gleixner 	return error;
6845cee9645SThomas Gleixner }
6855cee9645SThomas Gleixner 
6865cee9645SThomas Gleixner /*
6875cee9645SThomas Gleixner  * Locking issues: We need to protect the result of the id look up until
6885cee9645SThomas Gleixner  * we get the timer locked down so it is not deleted under us.  The
6895cee9645SThomas Gleixner  * removal is done under the idr spinlock so we use that here to bridge
6905cee9645SThomas Gleixner  * the find to the timer lock.  To avoid a dead lock, the timer id MUST
6915cee9645SThomas Gleixner  * be release with out holding the timer lock.
6925cee9645SThomas Gleixner  */
6935cee9645SThomas Gleixner static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
6945cee9645SThomas Gleixner {
6955cee9645SThomas Gleixner 	struct k_itimer *timr;
6965cee9645SThomas Gleixner 
6975cee9645SThomas Gleixner 	/*
6985cee9645SThomas Gleixner 	 * timer_t could be any type >= int and we want to make sure any
6995cee9645SThomas Gleixner 	 * @timer_id outside positive int range fails lookup.
7005cee9645SThomas Gleixner 	 */
7015cee9645SThomas Gleixner 	if ((unsigned long long)timer_id > INT_MAX)
7025cee9645SThomas Gleixner 		return NULL;
7035cee9645SThomas Gleixner 
7045cee9645SThomas Gleixner 	rcu_read_lock();
7055cee9645SThomas Gleixner 	timr = posix_timer_by_id(timer_id);
7065cee9645SThomas Gleixner 	if (timr) {
7075cee9645SThomas Gleixner 		spin_lock_irqsave(&timr->it_lock, *flags);
7085cee9645SThomas Gleixner 		if (timr->it_signal == current->signal) {
7095cee9645SThomas Gleixner 			rcu_read_unlock();
7105cee9645SThomas Gleixner 			return timr;
7115cee9645SThomas Gleixner 		}
7125cee9645SThomas Gleixner 		spin_unlock_irqrestore(&timr->it_lock, *flags);
7135cee9645SThomas Gleixner 	}
7145cee9645SThomas Gleixner 	rcu_read_unlock();
7155cee9645SThomas Gleixner 
7165cee9645SThomas Gleixner 	return NULL;
7175cee9645SThomas Gleixner }
7185cee9645SThomas Gleixner 
7195cee9645SThomas Gleixner /*
7205cee9645SThomas Gleixner  * Get the time remaining on a POSIX.1b interval timer.  This function
7215cee9645SThomas Gleixner  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
7225cee9645SThomas Gleixner  * mess with irq.
7235cee9645SThomas Gleixner  *
7245cee9645SThomas Gleixner  * We have a couple of messes to clean up here.  First there is the case
7255cee9645SThomas Gleixner  * of a timer that has a requeue pending.  These timers should appear to
7265cee9645SThomas Gleixner  * be in the timer list with an expiry as if we were to requeue them
7275cee9645SThomas Gleixner  * now.
7285cee9645SThomas Gleixner  *
7295cee9645SThomas Gleixner  * The second issue is the SIGEV_NONE timer which may be active but is
7305cee9645SThomas Gleixner  * not really ever put in the timer list (to save system resources).
7315cee9645SThomas Gleixner  * This timer may be expired, and if so, we will do it here.  Otherwise
7325cee9645SThomas Gleixner  * it is the same as a requeue pending timer WRT to what we should
7335cee9645SThomas Gleixner  * report.
7345cee9645SThomas Gleixner  */
7355cee9645SThomas Gleixner static void
7365cee9645SThomas Gleixner common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
7375cee9645SThomas Gleixner {
7385cee9645SThomas Gleixner 	ktime_t now, remaining, iv;
7395cee9645SThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
7405cee9645SThomas Gleixner 
7415cee9645SThomas Gleixner 	memset(cur_setting, 0, sizeof(struct itimerspec));
7425cee9645SThomas Gleixner 
7435cee9645SThomas Gleixner 	iv = timr->it.real.interval;
7445cee9645SThomas Gleixner 
7455cee9645SThomas Gleixner 	/* interval timer ? */
7462456e855SThomas Gleixner 	if (iv)
7475cee9645SThomas Gleixner 		cur_setting->it_interval = ktime_to_timespec(iv);
7485cee9645SThomas Gleixner 	else if (!hrtimer_active(timer) &&
7495cee9645SThomas Gleixner 		 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
7505cee9645SThomas Gleixner 		return;
7515cee9645SThomas Gleixner 
7525cee9645SThomas Gleixner 	now = timer->base->get_time();
7535cee9645SThomas Gleixner 
7545cee9645SThomas Gleixner 	/*
7555cee9645SThomas Gleixner 	 * When a requeue is pending or this is a SIGEV_NONE
7565cee9645SThomas Gleixner 	 * timer move the expiry time forward by intervals, so
7575cee9645SThomas Gleixner 	 * expiry is > now.
7585cee9645SThomas Gleixner 	 */
7592456e855SThomas Gleixner 	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
7605cee9645SThomas Gleixner 		   (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
7615cee9645SThomas Gleixner 		timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
7625cee9645SThomas Gleixner 
763572c3917SThomas Gleixner 	remaining = __hrtimer_expires_remaining_adjusted(timer, now);
7645cee9645SThomas Gleixner 	/* Return 0 only, when the timer is expired and not pending */
7652456e855SThomas Gleixner 	if (remaining <= 0) {
7665cee9645SThomas Gleixner 		/*
7675cee9645SThomas Gleixner 		 * A single shot SIGEV_NONE timer must return 0, when
7685cee9645SThomas Gleixner 		 * it is expired !
7695cee9645SThomas Gleixner 		 */
7705cee9645SThomas Gleixner 		if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
7715cee9645SThomas Gleixner 			cur_setting->it_value.tv_nsec = 1;
7725cee9645SThomas Gleixner 	} else
7735cee9645SThomas Gleixner 		cur_setting->it_value = ktime_to_timespec(remaining);
7745cee9645SThomas Gleixner }
7755cee9645SThomas Gleixner 
7765cee9645SThomas Gleixner /* Get the time remaining on a POSIX.1b interval timer. */
7775cee9645SThomas Gleixner SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
7785cee9645SThomas Gleixner 		struct itimerspec __user *, setting)
7795cee9645SThomas Gleixner {
7805cee9645SThomas Gleixner 	struct itimerspec cur_setting;
7815cee9645SThomas Gleixner 	struct k_itimer *timr;
7825cee9645SThomas Gleixner 	struct k_clock *kc;
7835cee9645SThomas Gleixner 	unsigned long flags;
7845cee9645SThomas Gleixner 	int ret = 0;
7855cee9645SThomas Gleixner 
7865cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flags);
7875cee9645SThomas Gleixner 	if (!timr)
7885cee9645SThomas Gleixner 		return -EINVAL;
7895cee9645SThomas Gleixner 
7905cee9645SThomas Gleixner 	kc = clockid_to_kclock(timr->it_clock);
7915cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_get))
7925cee9645SThomas Gleixner 		ret = -EINVAL;
7935cee9645SThomas Gleixner 	else
7945cee9645SThomas Gleixner 		kc->timer_get(timr, &cur_setting);
7955cee9645SThomas Gleixner 
7965cee9645SThomas Gleixner 	unlock_timer(timr, flags);
7975cee9645SThomas Gleixner 
7985cee9645SThomas Gleixner 	if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
7995cee9645SThomas Gleixner 		return -EFAULT;
8005cee9645SThomas Gleixner 
8015cee9645SThomas Gleixner 	return ret;
8025cee9645SThomas Gleixner }
8035cee9645SThomas Gleixner 
8045cee9645SThomas Gleixner /*
8055cee9645SThomas Gleixner  * Get the number of overruns of a POSIX.1b interval timer.  This is to
8065cee9645SThomas Gleixner  * be the overrun of the timer last delivered.  At the same time we are
8075cee9645SThomas Gleixner  * accumulating overruns on the next timer.  The overrun is frozen when
8085cee9645SThomas Gleixner  * the signal is delivered, either at the notify time (if the info block
8095cee9645SThomas Gleixner  * is not queued) or at the actual delivery time (as we are informed by
8105cee9645SThomas Gleixner  * the call back to do_schedule_next_timer().  So all we need to do is
8115cee9645SThomas Gleixner  * to pick up the frozen overrun.
8125cee9645SThomas Gleixner  */
8135cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
8145cee9645SThomas Gleixner {
8155cee9645SThomas Gleixner 	struct k_itimer *timr;
8165cee9645SThomas Gleixner 	int overrun;
8175cee9645SThomas Gleixner 	unsigned long flags;
8185cee9645SThomas Gleixner 
8195cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flags);
8205cee9645SThomas Gleixner 	if (!timr)
8215cee9645SThomas Gleixner 		return -EINVAL;
8225cee9645SThomas Gleixner 
8235cee9645SThomas Gleixner 	overrun = timr->it_overrun_last;
8245cee9645SThomas Gleixner 	unlock_timer(timr, flags);
8255cee9645SThomas Gleixner 
8265cee9645SThomas Gleixner 	return overrun;
8275cee9645SThomas Gleixner }
8285cee9645SThomas Gleixner 
8295cee9645SThomas Gleixner /* Set a POSIX.1b interval timer. */
8305cee9645SThomas Gleixner /* timr->it_lock is taken. */
8315cee9645SThomas Gleixner static int
8325cee9645SThomas Gleixner common_timer_set(struct k_itimer *timr, int flags,
8335cee9645SThomas Gleixner 		 struct itimerspec *new_setting, struct itimerspec *old_setting)
8345cee9645SThomas Gleixner {
8355cee9645SThomas Gleixner 	struct hrtimer *timer = &timr->it.real.timer;
8365cee9645SThomas Gleixner 	enum hrtimer_mode mode;
8375cee9645SThomas Gleixner 
8385cee9645SThomas Gleixner 	if (old_setting)
8395cee9645SThomas Gleixner 		common_timer_get(timr, old_setting);
8405cee9645SThomas Gleixner 
8415cee9645SThomas Gleixner 	/* disable the timer */
8422456e855SThomas Gleixner 	timr->it.real.interval = 0;
8435cee9645SThomas Gleixner 	/*
8445cee9645SThomas Gleixner 	 * careful here.  If smp we could be in the "fire" routine which will
8455cee9645SThomas Gleixner 	 * be spinning as we hold the lock.  But this is ONLY an SMP issue.
8465cee9645SThomas Gleixner 	 */
8475cee9645SThomas Gleixner 	if (hrtimer_try_to_cancel(timer) < 0)
8485cee9645SThomas Gleixner 		return TIMER_RETRY;
8495cee9645SThomas Gleixner 
8505cee9645SThomas Gleixner 	timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
8515cee9645SThomas Gleixner 		~REQUEUE_PENDING;
8525cee9645SThomas Gleixner 	timr->it_overrun_last = 0;
8535cee9645SThomas Gleixner 
8545cee9645SThomas Gleixner 	/* switch off the timer when it_value is zero */
8555cee9645SThomas Gleixner 	if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
8565cee9645SThomas Gleixner 		return 0;
8575cee9645SThomas Gleixner 
8585cee9645SThomas Gleixner 	mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
8595cee9645SThomas Gleixner 	hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
8605cee9645SThomas Gleixner 	timr->it.real.timer.function = posix_timer_fn;
8615cee9645SThomas Gleixner 
8625cee9645SThomas Gleixner 	hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
8635cee9645SThomas Gleixner 
8645cee9645SThomas Gleixner 	/* Convert interval */
8655cee9645SThomas Gleixner 	timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
8665cee9645SThomas Gleixner 
8675cee9645SThomas Gleixner 	/* SIGEV_NONE timers are not queued ! See common_timer_get */
8685cee9645SThomas Gleixner 	if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
8695cee9645SThomas Gleixner 		/* Setup correct expiry time for relative timers */
8705cee9645SThomas Gleixner 		if (mode == HRTIMER_MODE_REL) {
8715cee9645SThomas Gleixner 			hrtimer_add_expires(timer, timer->base->get_time());
8725cee9645SThomas Gleixner 		}
8735cee9645SThomas Gleixner 		return 0;
8745cee9645SThomas Gleixner 	}
8755cee9645SThomas Gleixner 
8765cee9645SThomas Gleixner 	hrtimer_start_expires(timer, mode);
8775cee9645SThomas Gleixner 	return 0;
8785cee9645SThomas Gleixner }
8795cee9645SThomas Gleixner 
8805cee9645SThomas Gleixner /* Set a POSIX.1b interval timer */
8815cee9645SThomas Gleixner SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
8825cee9645SThomas Gleixner 		const struct itimerspec __user *, new_setting,
8835cee9645SThomas Gleixner 		struct itimerspec __user *, old_setting)
8845cee9645SThomas Gleixner {
8855cee9645SThomas Gleixner 	struct k_itimer *timr;
8865cee9645SThomas Gleixner 	struct itimerspec new_spec, old_spec;
8875cee9645SThomas Gleixner 	int error = 0;
8885cee9645SThomas Gleixner 	unsigned long flag;
8895cee9645SThomas Gleixner 	struct itimerspec *rtn = old_setting ? &old_spec : NULL;
8905cee9645SThomas Gleixner 	struct k_clock *kc;
8915cee9645SThomas Gleixner 
8925cee9645SThomas Gleixner 	if (!new_setting)
8935cee9645SThomas Gleixner 		return -EINVAL;
8945cee9645SThomas Gleixner 
8955cee9645SThomas Gleixner 	if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
8965cee9645SThomas Gleixner 		return -EFAULT;
8975cee9645SThomas Gleixner 
8985cee9645SThomas Gleixner 	if (!timespec_valid(&new_spec.it_interval) ||
8995cee9645SThomas Gleixner 	    !timespec_valid(&new_spec.it_value))
9005cee9645SThomas Gleixner 		return -EINVAL;
9015cee9645SThomas Gleixner retry:
9025cee9645SThomas Gleixner 	timr = lock_timer(timer_id, &flag);
9035cee9645SThomas Gleixner 	if (!timr)
9045cee9645SThomas Gleixner 		return -EINVAL;
9055cee9645SThomas Gleixner 
9065cee9645SThomas Gleixner 	kc = clockid_to_kclock(timr->it_clock);
9075cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_set))
9085cee9645SThomas Gleixner 		error = -EINVAL;
9095cee9645SThomas Gleixner 	else
9105cee9645SThomas Gleixner 		error = kc->timer_set(timr, flags, &new_spec, rtn);
9115cee9645SThomas Gleixner 
9125cee9645SThomas Gleixner 	unlock_timer(timr, flag);
9135cee9645SThomas Gleixner 	if (error == TIMER_RETRY) {
9145cee9645SThomas Gleixner 		rtn = NULL;	// We already got the old time...
9155cee9645SThomas Gleixner 		goto retry;
9165cee9645SThomas Gleixner 	}
9175cee9645SThomas Gleixner 
9185cee9645SThomas Gleixner 	if (old_setting && !error &&
9195cee9645SThomas Gleixner 	    copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
9205cee9645SThomas Gleixner 		error = -EFAULT;
9215cee9645SThomas Gleixner 
9225cee9645SThomas Gleixner 	return error;
9235cee9645SThomas Gleixner }
9245cee9645SThomas Gleixner 
9255cee9645SThomas Gleixner static int common_timer_del(struct k_itimer *timer)
9265cee9645SThomas Gleixner {
9272456e855SThomas Gleixner 	timer->it.real.interval = 0;
9285cee9645SThomas Gleixner 
9295cee9645SThomas Gleixner 	if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
9305cee9645SThomas Gleixner 		return TIMER_RETRY;
9315cee9645SThomas Gleixner 	return 0;
9325cee9645SThomas Gleixner }
9335cee9645SThomas Gleixner 
9345cee9645SThomas Gleixner static inline int timer_delete_hook(struct k_itimer *timer)
9355cee9645SThomas Gleixner {
9365cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(timer->it_clock);
9375cee9645SThomas Gleixner 
9385cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->timer_del))
9395cee9645SThomas Gleixner 		return -EINVAL;
9405cee9645SThomas Gleixner 	return kc->timer_del(timer);
9415cee9645SThomas Gleixner }
9425cee9645SThomas Gleixner 
9435cee9645SThomas Gleixner /* Delete a POSIX.1b interval timer. */
9445cee9645SThomas Gleixner SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
9455cee9645SThomas Gleixner {
9465cee9645SThomas Gleixner 	struct k_itimer *timer;
9475cee9645SThomas Gleixner 	unsigned long flags;
9485cee9645SThomas Gleixner 
9495cee9645SThomas Gleixner retry_delete:
9505cee9645SThomas Gleixner 	timer = lock_timer(timer_id, &flags);
9515cee9645SThomas Gleixner 	if (!timer)
9525cee9645SThomas Gleixner 		return -EINVAL;
9535cee9645SThomas Gleixner 
9545cee9645SThomas Gleixner 	if (timer_delete_hook(timer) == TIMER_RETRY) {
9555cee9645SThomas Gleixner 		unlock_timer(timer, flags);
9565cee9645SThomas Gleixner 		goto retry_delete;
9575cee9645SThomas Gleixner 	}
9585cee9645SThomas Gleixner 
9595cee9645SThomas Gleixner 	spin_lock(&current->sighand->siglock);
9605cee9645SThomas Gleixner 	list_del(&timer->list);
9615cee9645SThomas Gleixner 	spin_unlock(&current->sighand->siglock);
9625cee9645SThomas Gleixner 	/*
9635cee9645SThomas Gleixner 	 * This keeps any tasks waiting on the spin lock from thinking
9645cee9645SThomas Gleixner 	 * they got something (see the lock code above).
9655cee9645SThomas Gleixner 	 */
9665cee9645SThomas Gleixner 	timer->it_signal = NULL;
9675cee9645SThomas Gleixner 
9685cee9645SThomas Gleixner 	unlock_timer(timer, flags);
9695cee9645SThomas Gleixner 	release_posix_timer(timer, IT_ID_SET);
9705cee9645SThomas Gleixner 	return 0;
9715cee9645SThomas Gleixner }
9725cee9645SThomas Gleixner 
9735cee9645SThomas Gleixner /*
9745cee9645SThomas Gleixner  * return timer owned by the process, used by exit_itimers
9755cee9645SThomas Gleixner  */
9765cee9645SThomas Gleixner static void itimer_delete(struct k_itimer *timer)
9775cee9645SThomas Gleixner {
9785cee9645SThomas Gleixner 	unsigned long flags;
9795cee9645SThomas Gleixner 
9805cee9645SThomas Gleixner retry_delete:
9815cee9645SThomas Gleixner 	spin_lock_irqsave(&timer->it_lock, flags);
9825cee9645SThomas Gleixner 
9835cee9645SThomas Gleixner 	if (timer_delete_hook(timer) == TIMER_RETRY) {
9845cee9645SThomas Gleixner 		unlock_timer(timer, flags);
9855cee9645SThomas Gleixner 		goto retry_delete;
9865cee9645SThomas Gleixner 	}
9875cee9645SThomas Gleixner 	list_del(&timer->list);
9885cee9645SThomas Gleixner 	/*
9895cee9645SThomas Gleixner 	 * This keeps any tasks waiting on the spin lock from thinking
9905cee9645SThomas Gleixner 	 * they got something (see the lock code above).
9915cee9645SThomas Gleixner 	 */
9925cee9645SThomas Gleixner 	timer->it_signal = NULL;
9935cee9645SThomas Gleixner 
9945cee9645SThomas Gleixner 	unlock_timer(timer, flags);
9955cee9645SThomas Gleixner 	release_posix_timer(timer, IT_ID_SET);
9965cee9645SThomas Gleixner }
9975cee9645SThomas Gleixner 
9985cee9645SThomas Gleixner /*
9995cee9645SThomas Gleixner  * This is called by do_exit or de_thread, only when there are no more
10005cee9645SThomas Gleixner  * references to the shared signal_struct.
10015cee9645SThomas Gleixner  */
10025cee9645SThomas Gleixner void exit_itimers(struct signal_struct *sig)
10035cee9645SThomas Gleixner {
10045cee9645SThomas Gleixner 	struct k_itimer *tmr;
10055cee9645SThomas Gleixner 
10065cee9645SThomas Gleixner 	while (!list_empty(&sig->posix_timers)) {
10075cee9645SThomas Gleixner 		tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
10085cee9645SThomas Gleixner 		itimer_delete(tmr);
10095cee9645SThomas Gleixner 	}
10105cee9645SThomas Gleixner }
10115cee9645SThomas Gleixner 
10125cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
10135cee9645SThomas Gleixner 		const struct timespec __user *, tp)
10145cee9645SThomas Gleixner {
10155cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
10165cee9645SThomas Gleixner 	struct timespec new_tp;
10175cee9645SThomas Gleixner 
10185cee9645SThomas Gleixner 	if (!kc || !kc->clock_set)
10195cee9645SThomas Gleixner 		return -EINVAL;
10205cee9645SThomas Gleixner 
10215cee9645SThomas Gleixner 	if (copy_from_user(&new_tp, tp, sizeof (*tp)))
10225cee9645SThomas Gleixner 		return -EFAULT;
10235cee9645SThomas Gleixner 
10245cee9645SThomas Gleixner 	return kc->clock_set(which_clock, &new_tp);
10255cee9645SThomas Gleixner }
10265cee9645SThomas Gleixner 
10275cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
10285cee9645SThomas Gleixner 		struct timespec __user *,tp)
10295cee9645SThomas Gleixner {
10305cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
10315cee9645SThomas Gleixner 	struct timespec kernel_tp;
10325cee9645SThomas Gleixner 	int error;
10335cee9645SThomas Gleixner 
10345cee9645SThomas Gleixner 	if (!kc)
10355cee9645SThomas Gleixner 		return -EINVAL;
10365cee9645SThomas Gleixner 
10375cee9645SThomas Gleixner 	error = kc->clock_get(which_clock, &kernel_tp);
10385cee9645SThomas Gleixner 
10395cee9645SThomas Gleixner 	if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
10405cee9645SThomas Gleixner 		error = -EFAULT;
10415cee9645SThomas Gleixner 
10425cee9645SThomas Gleixner 	return error;
10435cee9645SThomas Gleixner }
10445cee9645SThomas Gleixner 
10455cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
10465cee9645SThomas Gleixner 		struct timex __user *, utx)
10475cee9645SThomas Gleixner {
10485cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
10495cee9645SThomas Gleixner 	struct timex ktx;
10505cee9645SThomas Gleixner 	int err;
10515cee9645SThomas Gleixner 
10525cee9645SThomas Gleixner 	if (!kc)
10535cee9645SThomas Gleixner 		return -EINVAL;
10545cee9645SThomas Gleixner 	if (!kc->clock_adj)
10555cee9645SThomas Gleixner 		return -EOPNOTSUPP;
10565cee9645SThomas Gleixner 
10575cee9645SThomas Gleixner 	if (copy_from_user(&ktx, utx, sizeof(ktx)))
10585cee9645SThomas Gleixner 		return -EFAULT;
10595cee9645SThomas Gleixner 
10605cee9645SThomas Gleixner 	err = kc->clock_adj(which_clock, &ktx);
10615cee9645SThomas Gleixner 
10625cee9645SThomas Gleixner 	if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
10635cee9645SThomas Gleixner 		return -EFAULT;
10645cee9645SThomas Gleixner 
10655cee9645SThomas Gleixner 	return err;
10665cee9645SThomas Gleixner }
10675cee9645SThomas Gleixner 
10685cee9645SThomas Gleixner SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
10695cee9645SThomas Gleixner 		struct timespec __user *, tp)
10705cee9645SThomas Gleixner {
10715cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
10725cee9645SThomas Gleixner 	struct timespec rtn_tp;
10735cee9645SThomas Gleixner 	int error;
10745cee9645SThomas Gleixner 
10755cee9645SThomas Gleixner 	if (!kc)
10765cee9645SThomas Gleixner 		return -EINVAL;
10775cee9645SThomas Gleixner 
10785cee9645SThomas Gleixner 	error = kc->clock_getres(which_clock, &rtn_tp);
10795cee9645SThomas Gleixner 
10805cee9645SThomas Gleixner 	if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
10815cee9645SThomas Gleixner 		error = -EFAULT;
10825cee9645SThomas Gleixner 
10835cee9645SThomas Gleixner 	return error;
10845cee9645SThomas Gleixner }
10855cee9645SThomas Gleixner 
10865cee9645SThomas Gleixner /*
10875cee9645SThomas Gleixner  * nanosleep for monotonic and realtime clocks
10885cee9645SThomas Gleixner  */
10895cee9645SThomas Gleixner static int common_nsleep(const clockid_t which_clock, int flags,
10905cee9645SThomas Gleixner 			 struct timespec *tsave, struct timespec __user *rmtp)
10915cee9645SThomas Gleixner {
10925cee9645SThomas Gleixner 	return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
10935cee9645SThomas Gleixner 				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
10945cee9645SThomas Gleixner 				 which_clock);
10955cee9645SThomas Gleixner }
10965cee9645SThomas Gleixner 
10975cee9645SThomas Gleixner SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
10985cee9645SThomas Gleixner 		const struct timespec __user *, rqtp,
10995cee9645SThomas Gleixner 		struct timespec __user *, rmtp)
11005cee9645SThomas Gleixner {
11015cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
11025cee9645SThomas Gleixner 	struct timespec t;
11035cee9645SThomas Gleixner 
11045cee9645SThomas Gleixner 	if (!kc)
11055cee9645SThomas Gleixner 		return -EINVAL;
11065cee9645SThomas Gleixner 	if (!kc->nsleep)
11075cee9645SThomas Gleixner 		return -ENANOSLEEP_NOTSUP;
11085cee9645SThomas Gleixner 
11095cee9645SThomas Gleixner 	if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
11105cee9645SThomas Gleixner 		return -EFAULT;
11115cee9645SThomas Gleixner 
11125cee9645SThomas Gleixner 	if (!timespec_valid(&t))
11135cee9645SThomas Gleixner 		return -EINVAL;
11145cee9645SThomas Gleixner 
11155cee9645SThomas Gleixner 	return kc->nsleep(which_clock, flags, &t, rmtp);
11165cee9645SThomas Gleixner }
11175cee9645SThomas Gleixner 
11185cee9645SThomas Gleixner /*
11195cee9645SThomas Gleixner  * This will restart clock_nanosleep. This is required only by
11205cee9645SThomas Gleixner  * compat_clock_nanosleep_restart for now.
11215cee9645SThomas Gleixner  */
11225cee9645SThomas Gleixner long clock_nanosleep_restart(struct restart_block *restart_block)
11235cee9645SThomas Gleixner {
11245cee9645SThomas Gleixner 	clockid_t which_clock = restart_block->nanosleep.clockid;
11255cee9645SThomas Gleixner 	struct k_clock *kc = clockid_to_kclock(which_clock);
11265cee9645SThomas Gleixner 
11275cee9645SThomas Gleixner 	if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
11285cee9645SThomas Gleixner 		return -EINVAL;
11295cee9645SThomas Gleixner 
11305cee9645SThomas Gleixner 	return kc->nsleep_restart(restart_block);
11315cee9645SThomas Gleixner }
1132