Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/kernel/posix-timers.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * |
| 5 | * 2002-10-15 Posix Clocks & timers |
| 6 | * by George Anzinger george@mvista.com |
| 7 | * |
| 8 | * Copyright (C) 2002 2003 by MontaVista Software. |
| 9 | * |
| 10 | * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug. |
| 11 | * Copyright (C) 2004 Boris Hu |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or (at |
| 16 | * your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, but |
| 19 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | * |
| 27 | * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA |
| 28 | */ |
| 29 | |
| 30 | /* These are all the functions necessary to implement |
| 31 | * POSIX clocks & timers |
| 32 | */ |
| 33 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/time.h> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Ingo Molnar | 61855b6 | 2017-02-05 14:35:41 +0100 | [diff] [blame] | 38 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 40 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/list.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/compiler.h> |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 44 | #include <linux/hash.h> |
Richard Cochran | 0606f42 | 2011-02-01 13:52:35 +0000 | [diff] [blame] | 45 | #include <linux/posix-clock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/posix-timers.h> |
| 47 | #include <linux/syscalls.h> |
| 48 | #include <linux/wait.h> |
| 49 | #include <linux/workqueue.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 50 | #include <linux/export.h> |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 51 | #include <linux/hashtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 53 | #include "timekeeping.h" |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 56 | * Management arrays for POSIX timers. Timers are now kept in static hash table |
| 57 | * with 512 entries. |
| 58 | * Timer ids are allocated by local routine, which selects proper hash head by |
| 59 | * key, constructed from current->signal address and per signal struct counter. |
| 60 | * This keeps timer ids unique per process, but now they can intersect between |
| 61 | * processes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
| 63 | |
| 64 | /* |
| 65 | * Lets keep our timers in a slab cache :-) |
| 66 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 67 | static struct kmem_cache *posix_timers_cache; |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 68 | |
| 69 | static DEFINE_HASHTABLE(posix_timers_hashtable, 9); |
| 70 | static DEFINE_SPINLOCK(hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * we assume that the new SIGEV_THREAD_ID shares no bits with the other |
| 74 | * SIGEV values. Here we put out an error if this assumption fails. |
| 75 | */ |
| 76 | #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ |
| 77 | ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD)) |
| 78 | #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!" |
| 79 | #endif |
| 80 | |
Thomas Gleixner | 65da528 | 2011-02-01 13:51:01 +0000 | [diff] [blame] | 81 | /* |
| 82 | * parisc wants ENOTSUP instead of EOPNOTSUPP |
| 83 | */ |
| 84 | #ifndef ENOTSUP |
| 85 | # define ENANOSLEEP_NOTSUP EOPNOTSUPP |
| 86 | #else |
| 87 | # define ENANOSLEEP_NOTSUP ENOTSUP |
| 88 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * The timer ID is turned into a timer address by idr_find(). |
| 92 | * Verifying a valid ID consists of: |
| 93 | * |
| 94 | * a) checking that idr_find() returns other than -1. |
| 95 | * b) checking that the timer id matches the one in the timer itself. |
| 96 | * c) that the timer owner is in the callers thread group. |
| 97 | */ |
| 98 | |
| 99 | /* |
| 100 | * CLOCKs: The POSIX standard calls for a couple of clocks and allows us |
| 101 | * to implement others. This structure defines the various |
Richard Cochran | 0061748 | 2011-02-01 13:52:15 +0000 | [diff] [blame] | 102 | * clocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | * |
| 104 | * RESOLUTION: Clock resolution is used to round up timer and interval |
| 105 | * times, NOT to report clock times, which are reported with as |
| 106 | * much resolution as the system can muster. In some cases this |
| 107 | * resolution may depend on the underlying clock hardware and |
| 108 | * may not be quantifiable until run time, and only then is the |
| 109 | * necessary code is written. The standard says we should say |
| 110 | * something about this issue in the documentation... |
| 111 | * |
Richard Cochran | 0061748 | 2011-02-01 13:52:15 +0000 | [diff] [blame] | 112 | * FUNCTIONS: The CLOCKs structure defines possible functions to |
| 113 | * handle various clock functions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * |
Richard Cochran | 0061748 | 2011-02-01 13:52:15 +0000 | [diff] [blame] | 115 | * The standard POSIX timer management code assumes the |
| 116 | * following: 1.) The k_itimer struct (sched.h) is used for |
| 117 | * the timer. 2.) The list, it_lock, it_clock, it_id and |
| 118 | * it_pid fields are not modified by timer code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * |
| 120 | * Permissions: It is assumed that the clock_settime() function defined |
| 121 | * for each clock will take care of permission checks. Some |
| 122 | * clocks may be set able by any user (i.e. local process |
| 123 | * clocks) others not. Currently the only set able clock we |
| 124 | * have is CLOCK_REALTIME and its high res counter part, both of |
| 125 | * which we beg off on and pass to do_sys_settimeofday(). |
| 126 | */ |
| 127 | |
| 128 | static struct k_clock posix_clocks[MAX_CLOCKS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 130 | /* |
| 131 | * These ones are defined below. |
| 132 | */ |
| 133 | static int common_nsleep(const clockid_t, int flags, struct timespec *t, |
| 134 | struct timespec __user *rmtp); |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 135 | static int common_timer_create(struct k_itimer *new_timer); |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 136 | static void common_timer_get(struct k_itimer *, struct itimerspec *); |
| 137 | static int common_timer_set(struct k_itimer *, int, |
| 138 | struct itimerspec *, struct itimerspec *); |
| 139 | static int common_timer_del(struct k_itimer *timer); |
| 140 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 141 | static enum hrtimer_restart posix_timer_fn(struct hrtimer *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Namhyung Kim | 20f33a0 | 2010-10-20 15:57:34 -0700 | [diff] [blame] | 143 | static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags); |
| 144 | |
| 145 | #define lock_timer(tid, flags) \ |
| 146 | ({ struct k_itimer *__timr; \ |
| 147 | __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \ |
| 148 | __timr; \ |
| 149 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 151 | static int hash(struct signal_struct *sig, unsigned int nr) |
| 152 | { |
| 153 | return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable)); |
| 154 | } |
| 155 | |
| 156 | static struct k_itimer *__posix_timers_find(struct hlist_head *head, |
| 157 | struct signal_struct *sig, |
| 158 | timer_t id) |
| 159 | { |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 160 | struct k_itimer *timer; |
| 161 | |
| 162 | hlist_for_each_entry_rcu(timer, head, t_hash) { |
| 163 | if ((timer->it_signal == sig) && (timer->it_id == id)) |
| 164 | return timer; |
| 165 | } |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | static struct k_itimer *posix_timer_by_id(timer_t id) |
| 170 | { |
| 171 | struct signal_struct *sig = current->signal; |
| 172 | struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)]; |
| 173 | |
| 174 | return __posix_timers_find(head, sig, id); |
| 175 | } |
| 176 | |
| 177 | static int posix_timer_add(struct k_itimer *timer) |
| 178 | { |
| 179 | struct signal_struct *sig = current->signal; |
| 180 | int first_free_id = sig->posix_timer_id; |
| 181 | struct hlist_head *head; |
| 182 | int ret = -ENOENT; |
| 183 | |
| 184 | do { |
| 185 | spin_lock(&hash_lock); |
| 186 | head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)]; |
| 187 | if (!__posix_timers_find(head, sig, sig->posix_timer_id)) { |
| 188 | hlist_add_head_rcu(&timer->t_hash, head); |
| 189 | ret = sig->posix_timer_id; |
| 190 | } |
| 191 | if (++sig->posix_timer_id < 0) |
| 192 | sig->posix_timer_id = 0; |
| 193 | if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT)) |
| 194 | /* Loop over all possible ids completed */ |
| 195 | ret = -EAGAIN; |
| 196 | spin_unlock(&hash_lock); |
| 197 | } while (ret == -ENOENT); |
| 198 | return ret; |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) |
| 202 | { |
| 203 | spin_unlock_irqrestore(&timr->it_lock, flags); |
| 204 | } |
| 205 | |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 206 | /* Get clock_realtime */ |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 207 | static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp) |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 208 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 209 | ktime_get_real_ts64(tp); |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 213 | /* Set clock_realtime */ |
| 214 | static int posix_clock_realtime_set(const clockid_t which_clock, |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame^] | 215 | const struct timespec64 *tp) |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 216 | { |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame^] | 217 | return do_sys_settimeofday64(tp, NULL); |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Richard Cochran | f1f1d5e | 2011-02-01 13:52:26 +0000 | [diff] [blame] | 220 | static int posix_clock_realtime_adj(const clockid_t which_clock, |
| 221 | struct timex *t) |
| 222 | { |
| 223 | return do_adjtimex(t); |
| 224 | } |
| 225 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 226 | /* |
| 227 | * Get monotonic time for posix timers |
| 228 | */ |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 229 | static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 230 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 231 | ktime_get_ts64(tp); |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | /* |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 236 | * Get monotonic-raw time for posix timers |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 237 | */ |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 238 | static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 239 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 240 | getrawmonotonic64(tp); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 244 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 245 | static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 246 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 247 | *tp = current_kernel_time64(); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int posix_get_monotonic_coarse(clockid_t which_clock, |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 252 | struct timespec64 *tp) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 253 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 254 | *tp = get_monotonic_coarse64(); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 258 | static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 259 | { |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 260 | *tp = ktime_to_timespec64(KTIME_LOW_RES); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 261 | return 0; |
| 262 | } |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 263 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 264 | static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp) |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 265 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 266 | get_monotonic_boottime64(tp); |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 270 | static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp) |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 271 | { |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 272 | timekeeping_clocktai64(tp); |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 275 | |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 276 | static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp) |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 277 | { |
| 278 | tp->tv_sec = 0; |
| 279 | tp->tv_nsec = hrtimer_resolution; |
| 280 | return 0; |
| 281 | } |
| 282 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 283 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * Initialize everything, well, just everything in Posix clocks/timers ;) |
| 285 | */ |
| 286 | static __init int init_posix_timers(void) |
| 287 | { |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 288 | struct k_clock clock_realtime = { |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 289 | .clock_getres = posix_get_hrtimer_res, |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 290 | .clock_get = posix_clock_realtime_get, |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 291 | .clock_set = posix_clock_realtime_set, |
Richard Cochran | f1f1d5e | 2011-02-01 13:52:26 +0000 | [diff] [blame] | 292 | .clock_adj = posix_clock_realtime_adj, |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 293 | .nsleep = common_nsleep, |
Thomas Gleixner | 59bd5bc | 2011-02-01 13:51:17 +0000 | [diff] [blame] | 294 | .nsleep_restart = hrtimer_nanosleep_restart, |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 295 | .timer_create = common_timer_create, |
Thomas Gleixner | 27722df | 2011-02-01 13:52:01 +0000 | [diff] [blame] | 296 | .timer_set = common_timer_set, |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 297 | .timer_get = common_timer_get, |
Thomas Gleixner | 6761c67 | 2011-02-01 13:52:07 +0000 | [diff] [blame] | 298 | .timer_del = common_timer_del, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | }; |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 300 | struct k_clock clock_monotonic = { |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 301 | .clock_getres = posix_get_hrtimer_res, |
Thomas Gleixner | 2fd1f04 | 2011-02-01 13:51:03 +0000 | [diff] [blame] | 302 | .clock_get = posix_ktime_get_ts, |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 303 | .nsleep = common_nsleep, |
Thomas Gleixner | 59bd5bc | 2011-02-01 13:51:17 +0000 | [diff] [blame] | 304 | .nsleep_restart = hrtimer_nanosleep_restart, |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 305 | .timer_create = common_timer_create, |
Thomas Gleixner | 27722df | 2011-02-01 13:52:01 +0000 | [diff] [blame] | 306 | .timer_set = common_timer_set, |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 307 | .timer_get = common_timer_get, |
Thomas Gleixner | 6761c67 | 2011-02-01 13:52:07 +0000 | [diff] [blame] | 308 | .timer_del = common_timer_del, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | }; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 310 | struct k_clock clock_monotonic_raw = { |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 311 | .clock_getres = posix_get_hrtimer_res, |
Thomas Gleixner | 2fd1f04 | 2011-02-01 13:51:03 +0000 | [diff] [blame] | 312 | .clock_get = posix_get_monotonic_raw, |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 313 | }; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 314 | struct k_clock clock_realtime_coarse = { |
Thomas Gleixner | 2fd1f04 | 2011-02-01 13:51:03 +0000 | [diff] [blame] | 315 | .clock_getres = posix_get_coarse_res, |
| 316 | .clock_get = posix_get_realtime_coarse, |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 317 | }; |
| 318 | struct k_clock clock_monotonic_coarse = { |
Thomas Gleixner | 2fd1f04 | 2011-02-01 13:51:03 +0000 | [diff] [blame] | 319 | .clock_getres = posix_get_coarse_res, |
| 320 | .clock_get = posix_get_monotonic_coarse, |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 321 | }; |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 322 | struct k_clock clock_tai = { |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 323 | .clock_getres = posix_get_hrtimer_res, |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 324 | .clock_get = posix_get_tai, |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 325 | .nsleep = common_nsleep, |
| 326 | .nsleep_restart = hrtimer_nanosleep_restart, |
| 327 | .timer_create = common_timer_create, |
| 328 | .timer_set = common_timer_set, |
| 329 | .timer_get = common_timer_get, |
| 330 | .timer_del = common_timer_del, |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 331 | }; |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 332 | struct k_clock clock_boottime = { |
Thomas Gleixner | 056a3ca | 2015-04-14 21:08:32 +0000 | [diff] [blame] | 333 | .clock_getres = posix_get_hrtimer_res, |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 334 | .clock_get = posix_get_boottime, |
| 335 | .nsleep = common_nsleep, |
| 336 | .nsleep_restart = hrtimer_nanosleep_restart, |
| 337 | .timer_create = common_timer_create, |
| 338 | .timer_set = common_timer_set, |
| 339 | .timer_get = common_timer_get, |
| 340 | .timer_del = common_timer_del, |
| 341 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Thomas Gleixner | 5270873 | 2011-02-02 12:10:09 +0100 | [diff] [blame] | 343 | posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime); |
| 344 | posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic); |
| 345 | posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw); |
| 346 | posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse); |
| 347 | posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse); |
John Stultz | 7fdd7f8 | 2011-02-15 10:52:57 -0800 | [diff] [blame] | 348 | posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime); |
John Stultz | 1ff3c96 | 2012-05-03 12:43:40 -0700 | [diff] [blame] | 349 | posix_timers_register_clock(CLOCK_TAI, &clock_tai); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | posix_timers_cache = kmem_cache_create("posix_timers_cache", |
Alexey Dobriyan | 040b5c6 | 2007-10-16 23:26:10 -0700 | [diff] [blame] | 352 | sizeof (struct k_itimer), 0, SLAB_PANIC, |
| 353 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | __initcall(init_posix_timers); |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | static void schedule_next_timer(struct k_itimer *timr) |
| 360 | { |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 361 | struct hrtimer *timer = &timr->it.real.timer; |
| 362 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 363 | if (timr->it.real.interval == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | return; |
| 365 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 366 | timr->it_overrun += (unsigned int) hrtimer_forward(timer, |
| 367 | timer->base->get_time(), |
| 368 | timr->it.real.interval); |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | timr->it_overrun_last = timr->it_overrun; |
| 371 | timr->it_overrun = -1; |
| 372 | ++timr->it_requeue_pending; |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 373 | hrtimer_restart(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
| 377 | * This function is exported for use by the signal deliver code. It is |
| 378 | * called just prior to the info block being released and passes that |
| 379 | * block to us. It's function is to update the overrun entry AND to |
| 380 | * restart the timer. It should only be called if the timer is to be |
| 381 | * restarted (i.e. we have flagged this in the sys_private entry of the |
| 382 | * info block). |
| 383 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 384 | * To protect against the timer going away while the interrupt is queued, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * we require that the it_requeue_pending flag be set. |
| 386 | */ |
| 387 | void do_schedule_next_timer(struct siginfo *info) |
| 388 | { |
| 389 | struct k_itimer *timr; |
| 390 | unsigned long flags; |
| 391 | |
| 392 | timr = lock_timer(info->si_tid, &flags); |
| 393 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 394 | if (timr && timr->it_requeue_pending == info->si_sys_private) { |
| 395 | if (timr->it_clock < 0) |
| 396 | posix_cpu_timer_schedule(timr); |
| 397 | else |
| 398 | schedule_next_timer(timr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Oleg Nesterov | 54da117 | 2008-07-23 20:52:05 +0400 | [diff] [blame] | 400 | info->si_overrun += timr->it_overrun_last; |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Thomas Gleixner | b6557fb | 2006-02-01 03:05:09 -0800 | [diff] [blame] | 403 | if (timr) |
| 404 | unlock_timer(timr, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Oleg Nesterov | ba66129 | 2008-07-23 20:52:05 +0400 | [diff] [blame] | 407 | int posix_timer_event(struct k_itimer *timr, int si_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
Oleg Nesterov | 27af424 | 2008-12-01 14:18:13 -0800 | [diff] [blame] | 409 | struct task_struct *task; |
| 410 | int shared, ret = -1; |
Oleg Nesterov | ba66129 | 2008-07-23 20:52:05 +0400 | [diff] [blame] | 411 | /* |
| 412 | * FIXME: if ->sigq is queued we can race with |
| 413 | * dequeue_signal()->do_schedule_next_timer(). |
| 414 | * |
| 415 | * If dequeue_signal() sees the "right" value of |
| 416 | * si_sys_private it calls do_schedule_next_timer(). |
| 417 | * We re-queue ->sigq and drop ->it_lock(). |
| 418 | * do_schedule_next_timer() locks the timer |
| 419 | * and re-schedules it while ->sigq is pending. |
| 420 | * Not really bad, but not that we want. |
| 421 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | timr->sigq->info.si_sys_private = si_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Oleg Nesterov | 27af424 | 2008-12-01 14:18:13 -0800 | [diff] [blame] | 424 | rcu_read_lock(); |
| 425 | task = pid_task(timr->it_pid, PIDTYPE_PID); |
| 426 | if (task) { |
| 427 | shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID); |
| 428 | ret = send_sigqueue(timr->sigq, task, shared); |
| 429 | } |
| 430 | rcu_read_unlock(); |
Oleg Nesterov | 4aa7361 | 2008-09-22 14:42:46 -0700 | [diff] [blame] | 431 | /* If we failed to send the signal the timer stops. */ |
| 432 | return ret > 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | EXPORT_SYMBOL_GPL(posix_timer_event); |
| 435 | |
| 436 | /* |
| 437 | * This function gets called when a POSIX.1b interval timer expires. It |
| 438 | * is used as a callback from the kernel internal timer. The |
| 439 | * run_timer_list code ALWAYS calls with interrupts on. |
| 440 | |
| 441 | * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. |
| 442 | */ |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 443 | static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame] | 445 | struct k_itimer *timr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | unsigned long flags; |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 447 | int si_private = 0; |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 448 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame] | 450 | timr = container_of(timer, struct k_itimer, it.real.timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | spin_lock_irqsave(&timr->it_lock, flags); |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 452 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 453 | if (timr->it.real.interval != 0) |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 454 | si_private = ++timr->it_requeue_pending; |
| 455 | |
| 456 | if (posix_timer_event(timr, si_private)) { |
| 457 | /* |
| 458 | * signal was not sent because of sig_ignor |
| 459 | * we will not get a call back to restart it AND |
| 460 | * it should be restarted. |
| 461 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 462 | if (timr->it.real.interval != 0) { |
Thomas Gleixner | 58229a1 | 2007-06-21 20:45:15 +0000 | [diff] [blame] | 463 | ktime_t now = hrtimer_cb_get_time(timer); |
| 464 | |
| 465 | /* |
| 466 | * FIXME: What we really want, is to stop this |
| 467 | * timer completely and restart it in case the |
| 468 | * SIG_IGN is removed. This is a non trivial |
| 469 | * change which involves sighand locking |
| 470 | * (sigh !), which we don't want to do late in |
| 471 | * the release cycle. |
| 472 | * |
| 473 | * For now we just let timers with an interval |
| 474 | * less than a jiffie expire every jiffie to |
| 475 | * avoid softirq starvation in case of SIG_IGN |
| 476 | * and a very small interval, which would put |
| 477 | * the timer right back on the softirq pending |
| 478 | * list. By moving now ahead of time we trick |
| 479 | * hrtimer_forward() to expire the timer |
| 480 | * later, while we still maintain the overrun |
| 481 | * accuracy, but have some inconsistency in |
| 482 | * the timer_gettime() case. This is at least |
| 483 | * better than a starved softirq. A more |
| 484 | * complex fix which solves also another related |
| 485 | * inconsistency is already in the pipeline. |
| 486 | */ |
| 487 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 488 | { |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 489 | ktime_t kj = NSEC_PER_SEC / HZ; |
Thomas Gleixner | 58229a1 | 2007-06-21 20:45:15 +0000 | [diff] [blame] | 490 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 491 | if (timr->it.real.interval < kj) |
Thomas Gleixner | 58229a1 | 2007-06-21 20:45:15 +0000 | [diff] [blame] | 492 | now = ktime_add(now, kj); |
| 493 | } |
| 494 | #endif |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 495 | timr->it_overrun += (unsigned int) |
Thomas Gleixner | 58229a1 | 2007-06-21 20:45:15 +0000 | [diff] [blame] | 496 | hrtimer_forward(timer, now, |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 497 | timr->it.real.interval); |
| 498 | ret = HRTIMER_RESTART; |
Roman Zippel | a0a0c28 | 2006-03-16 23:04:01 -0800 | [diff] [blame] | 499 | ++timr->it_requeue_pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 503 | unlock_timer(timr, flags); |
| 504 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Oleg Nesterov | 27af424 | 2008-12-01 14:18:13 -0800 | [diff] [blame] | 507 | static struct pid *good_sigevent(sigevent_t * event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
| 509 | struct task_struct *rtn = current->group_leader; |
| 510 | |
| 511 | if ((event->sigev_notify & SIGEV_THREAD_ID ) && |
Pavel Emelyanov | 8dc86af | 2008-02-08 04:21:52 -0800 | [diff] [blame] | 512 | (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) || |
Pavel Emelyanov | bac0abd | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 513 | !same_thread_group(rtn, current) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL)) |
| 515 | return NULL; |
| 516 | |
| 517 | if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) && |
| 518 | ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX))) |
| 519 | return NULL; |
| 520 | |
Oleg Nesterov | 27af424 | 2008-12-01 14:18:13 -0800 | [diff] [blame] | 521 | return task_pid(rtn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Thomas Gleixner | 5270873 | 2011-02-02 12:10:09 +0100 | [diff] [blame] | 524 | void posix_timers_register_clock(const clockid_t clock_id, |
| 525 | struct k_clock *new_clock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
| 527 | if ((unsigned) clock_id >= MAX_CLOCKS) { |
Thomas Gleixner | 4359ac0 | 2011-02-02 11:45:23 +0100 | [diff] [blame] | 528 | printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n", |
| 529 | clock_id); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if (!new_clock->clock_get) { |
| 534 | printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n", |
| 535 | clock_id); |
| 536 | return; |
| 537 | } |
| 538 | if (!new_clock->clock_getres) { |
| 539 | printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | clock_id); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | posix_clocks[clock_id] = *new_clock; |
| 545 | } |
Thomas Gleixner | 5270873 | 2011-02-02 12:10:09 +0100 | [diff] [blame] | 546 | EXPORT_SYMBOL_GPL(posix_timers_register_clock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | static struct k_itimer * alloc_posix_timer(void) |
| 549 | { |
| 550 | struct k_itimer *tmr; |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 551 | tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (!tmr) |
| 553 | return tmr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { |
| 555 | kmem_cache_free(posix_timers_cache, tmr); |
Dan Carpenter | aa94fbd | 2008-10-02 14:50:14 -0700 | [diff] [blame] | 556 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
Oleg Nesterov | ba66129 | 2008-07-23 20:52:05 +0400 | [diff] [blame] | 558 | memset(&tmr->sigq->info, 0, sizeof(siginfo_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return tmr; |
| 560 | } |
| 561 | |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 562 | static void k_itimer_rcu_free(struct rcu_head *head) |
| 563 | { |
| 564 | struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu); |
| 565 | |
| 566 | kmem_cache_free(posix_timers_cache, tmr); |
| 567 | } |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | #define IT_ID_SET 1 |
| 570 | #define IT_ID_NOT_SET 0 |
| 571 | static void release_posix_timer(struct k_itimer *tmr, int it_id_set) |
| 572 | { |
| 573 | if (it_id_set) { |
| 574 | unsigned long flags; |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 575 | spin_lock_irqsave(&hash_lock, flags); |
| 576 | hlist_del_rcu(&tmr->t_hash); |
| 577 | spin_unlock_irqrestore(&hash_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 579 | put_pid(tmr->it_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | sigqueue_free(tmr->sigq); |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 581 | call_rcu(&tmr->it.rcu, k_itimer_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Thomas Gleixner | cc785ac | 2011-02-01 13:51:09 +0000 | [diff] [blame] | 584 | static struct k_clock *clockid_to_kclock(const clockid_t id) |
| 585 | { |
| 586 | if (id < 0) |
Richard Cochran | 0606f42 | 2011-02-01 13:52:35 +0000 | [diff] [blame] | 587 | return (id & CLOCKFD_MASK) == CLOCKFD ? |
| 588 | &clock_posix_dynamic : &clock_posix_cpu; |
Thomas Gleixner | cc785ac | 2011-02-01 13:51:09 +0000 | [diff] [blame] | 589 | |
| 590 | if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres) |
| 591 | return NULL; |
| 592 | return &posix_clocks[id]; |
| 593 | } |
| 594 | |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 595 | static int common_timer_create(struct k_itimer *new_timer) |
| 596 | { |
| 597 | hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); |
| 598 | return 0; |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /* Create a POSIX.1b interval timer. */ |
| 602 | |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 603 | SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, |
| 604 | struct sigevent __user *, timer_event_spec, |
| 605 | timer_t __user *, created_timer_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 607 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Oleg Nesterov | 2cd499e | 2008-09-22 14:42:47 -0700 | [diff] [blame] | 608 | struct k_itimer *new_timer; |
Oleg Nesterov | ef864c9 | 2008-09-22 14:42:49 -0700 | [diff] [blame] | 609 | int error, new_timer_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | sigevent_t event; |
| 611 | int it_id_set = IT_ID_NOT_SET; |
| 612 | |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 613 | if (!kc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return -EINVAL; |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 615 | if (!kc->timer_create) |
| 616 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | new_timer = alloc_posix_timer(); |
| 619 | if (unlikely(!new_timer)) |
| 620 | return -EAGAIN; |
| 621 | |
| 622 | spin_lock_init(&new_timer->it_lock); |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 623 | new_timer_id = posix_timer_add(new_timer); |
| 624 | if (new_timer_id < 0) { |
| 625 | error = new_timer_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | goto out; |
| 627 | } |
| 628 | |
| 629 | it_id_set = IT_ID_SET; |
| 630 | new_timer->it_id = (timer_t) new_timer_id; |
| 631 | new_timer->it_clock = which_clock; |
| 632 | new_timer->it_overrun = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (timer_event_spec) { |
| 635 | if (copy_from_user(&event, timer_event_spec, sizeof (event))) { |
| 636 | error = -EFAULT; |
| 637 | goto out; |
| 638 | } |
Oleg Nesterov | 36b2f04 | 2008-09-22 14:42:48 -0700 | [diff] [blame] | 639 | rcu_read_lock(); |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 640 | new_timer->it_pid = get_pid(good_sigevent(&event)); |
Oleg Nesterov | 36b2f04 | 2008-09-22 14:42:48 -0700 | [diff] [blame] | 641 | rcu_read_unlock(); |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 642 | if (!new_timer->it_pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | error = -EINVAL; |
| 644 | goto out; |
| 645 | } |
| 646 | } else { |
Mathias Krause | 6891c45 | 2014-10-04 23:06:39 +0200 | [diff] [blame] | 647 | memset(&event.sigev_value, 0, sizeof(event.sigev_value)); |
Oleg Nesterov | 5a9fa73 | 2008-09-22 14:42:50 -0700 | [diff] [blame] | 648 | event.sigev_notify = SIGEV_SIGNAL; |
| 649 | event.sigev_signo = SIGALRM; |
| 650 | event.sigev_value.sival_int = new_timer->it_id; |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 651 | new_timer->it_pid = get_pid(task_tgid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Oleg Nesterov | 5a9fa73 | 2008-09-22 14:42:50 -0700 | [diff] [blame] | 654 | new_timer->it_sigev_notify = event.sigev_notify; |
| 655 | new_timer->sigq->info.si_signo = event.sigev_signo; |
| 656 | new_timer->sigq->info.si_value = event.sigev_value; |
Oleg Nesterov | 717835d | 2008-09-22 14:42:49 -0700 | [diff] [blame] | 657 | new_timer->sigq->info.si_tid = new_timer->it_id; |
Oleg Nesterov | 5a9fa73 | 2008-09-22 14:42:50 -0700 | [diff] [blame] | 658 | new_timer->sigq->info.si_code = SI_TIMER; |
Oleg Nesterov | 717835d | 2008-09-22 14:42:49 -0700 | [diff] [blame] | 659 | |
Andrey Vagin | 2b08de0 | 2010-07-20 15:23:14 -0700 | [diff] [blame] | 660 | if (copy_to_user(created_timer_id, |
| 661 | &new_timer_id, sizeof (new_timer_id))) { |
| 662 | error = -EFAULT; |
| 663 | goto out; |
| 664 | } |
| 665 | |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 666 | error = kc->timer_create(new_timer); |
Andrey Vagin | 45e0fff | 2010-05-24 12:15:33 -0700 | [diff] [blame] | 667 | if (error) |
| 668 | goto out; |
| 669 | |
Oleg Nesterov | 36b2f04 | 2008-09-22 14:42:48 -0700 | [diff] [blame] | 670 | spin_lock_irq(¤t->sighand->siglock); |
Oleg Nesterov | 27af424 | 2008-12-01 14:18:13 -0800 | [diff] [blame] | 671 | new_timer->it_signal = current->signal; |
Oleg Nesterov | 36b2f04 | 2008-09-22 14:42:48 -0700 | [diff] [blame] | 672 | list_add(&new_timer->list, ¤t->signal->posix_timers); |
| 673 | spin_unlock_irq(¤t->sighand->siglock); |
Oleg Nesterov | ef864c9 | 2008-09-22 14:42:49 -0700 | [diff] [blame] | 674 | |
| 675 | return 0; |
Thomas Gleixner | 838394f | 2011-02-01 13:51:58 +0000 | [diff] [blame] | 676 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | * In the case of the timer belonging to another task, after |
| 678 | * the task is unlocked, the timer is owned by the other task |
| 679 | * and may cease to exist at any time. Don't use or modify |
| 680 | * new_timer after the unlock call. |
| 681 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | out: |
Oleg Nesterov | ef864c9 | 2008-09-22 14:42:49 -0700 | [diff] [blame] | 683 | release_posix_timer(new_timer, it_id_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return error; |
| 685 | } |
| 686 | |
| 687 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | * Locking issues: We need to protect the result of the id look up until |
| 689 | * we get the timer locked down so it is not deleted under us. The |
| 690 | * removal is done under the idr spinlock so we use that here to bridge |
| 691 | * the find to the timer lock. To avoid a dead lock, the timer id MUST |
| 692 | * be release with out holding the timer lock. |
| 693 | */ |
Namhyung Kim | 20f33a0 | 2010-10-20 15:57:34 -0700 | [diff] [blame] | 694 | static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
| 696 | struct k_itimer *timr; |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 697 | |
Tejun Heo | e182bb3 | 2013-02-20 15:24:12 -0800 | [diff] [blame] | 698 | /* |
| 699 | * timer_t could be any type >= int and we want to make sure any |
| 700 | * @timer_id outside positive int range fails lookup. |
| 701 | */ |
| 702 | if ((unsigned long long)timer_id > INT_MAX) |
| 703 | return NULL; |
| 704 | |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 705 | rcu_read_lock(); |
Pavel Emelyanov | 5ed67f0 | 2013-03-11 13:12:21 +0400 | [diff] [blame] | 706 | timr = posix_timer_by_id(timer_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (timr) { |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 708 | spin_lock_irqsave(&timr->it_lock, *flags); |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 709 | if (timr->it_signal == current->signal) { |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 710 | rcu_read_unlock(); |
Oleg Nesterov | 31d9284 | 2008-09-22 14:42:51 -0700 | [diff] [blame] | 711 | return timr; |
| 712 | } |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 713 | spin_unlock_irqrestore(&timr->it_lock, *flags); |
Oleg Nesterov | 31d9284 | 2008-09-22 14:42:51 -0700 | [diff] [blame] | 714 | } |
Eric Dumazet | 8af0887 | 2011-05-24 11:12:58 +0200 | [diff] [blame] | 715 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Oleg Nesterov | 31d9284 | 2008-09-22 14:42:51 -0700 | [diff] [blame] | 717 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Get the time remaining on a POSIX.1b interval timer. This function |
| 722 | * is ALWAYS called with spin_lock_irq on the timer, thus it must not |
| 723 | * mess with irq. |
| 724 | * |
| 725 | * We have a couple of messes to clean up here. First there is the case |
| 726 | * of a timer that has a requeue pending. These timers should appear to |
| 727 | * be in the timer list with an expiry as if we were to requeue them |
| 728 | * now. |
| 729 | * |
| 730 | * The second issue is the SIGEV_NONE timer which may be active but is |
| 731 | * not really ever put in the timer list (to save system resources). |
| 732 | * This timer may be expired, and if so, we will do it here. Otherwise |
| 733 | * it is the same as a requeue pending timer WRT to what we should |
| 734 | * report. |
| 735 | */ |
| 736 | static void |
| 737 | common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) |
| 738 | { |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 739 | ktime_t now, remaining, iv; |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 740 | struct hrtimer *timer = &timr->it.real.timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 742 | memset(cur_setting, 0, sizeof(struct itimerspec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 744 | iv = timr->it.real.interval; |
| 745 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 746 | /* interval timer ? */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 747 | if (iv) |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 748 | cur_setting->it_interval = ktime_to_timespec(iv); |
| 749 | else if (!hrtimer_active(timer) && |
| 750 | (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 751 | return; |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 752 | |
| 753 | now = timer->base->get_time(); |
| 754 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 755 | /* |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 756 | * When a requeue is pending or this is a SIGEV_NONE |
| 757 | * timer move the expiry time forward by intervals, so |
| 758 | * expiry is > now. |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 759 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 760 | if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || |
| 761 | (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 762 | timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv); |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 763 | |
Thomas Gleixner | 572c391 | 2016-01-14 16:54:47 +0000 | [diff] [blame] | 764 | remaining = __hrtimer_expires_remaining_adjusted(timer, now); |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 765 | /* Return 0 only, when the timer is expired and not pending */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 766 | if (remaining <= 0) { |
Roman Zippel | 3b98a53 | 2006-03-26 01:38:07 -0800 | [diff] [blame] | 767 | /* |
| 768 | * A single shot SIGEV_NONE timer must return 0, when |
| 769 | * it is expired ! |
| 770 | */ |
| 771 | if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) |
| 772 | cur_setting->it_value.tv_nsec = 1; |
| 773 | } else |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 774 | cur_setting->it_value = ktime_to_timespec(remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* Get the time remaining on a POSIX.1b interval timer. */ |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 778 | SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, |
| 779 | struct itimerspec __user *, setting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | struct itimerspec cur_setting; |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 782 | struct k_itimer *timr; |
| 783 | struct k_clock *kc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | unsigned long flags; |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 785 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
| 787 | timr = lock_timer(timer_id, &flags); |
| 788 | if (!timr) |
| 789 | return -EINVAL; |
| 790 | |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 791 | kc = clockid_to_kclock(timr->it_clock); |
| 792 | if (WARN_ON_ONCE(!kc || !kc->timer_get)) |
| 793 | ret = -EINVAL; |
| 794 | else |
| 795 | kc->timer_get(timr, &cur_setting); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | unlock_timer(timr, flags); |
| 798 | |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 799 | if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | return -EFAULT; |
| 801 | |
Thomas Gleixner | a7319fa | 2011-02-01 13:52:04 +0000 | [diff] [blame] | 802 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* |
| 806 | * Get the number of overruns of a POSIX.1b interval timer. This is to |
| 807 | * be the overrun of the timer last delivered. At the same time we are |
| 808 | * accumulating overruns on the next timer. The overrun is frozen when |
| 809 | * the signal is delivered, either at the notify time (if the info block |
| 810 | * is not queued) or at the actual delivery time (as we are informed by |
| 811 | * the call back to do_schedule_next_timer(). So all we need to do is |
| 812 | * to pick up the frozen overrun. |
| 813 | */ |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 814 | SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
| 816 | struct k_itimer *timr; |
| 817 | int overrun; |
Al Viro | 5ba2533 | 2007-10-14 19:35:50 +0100 | [diff] [blame] | 818 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
| 820 | timr = lock_timer(timer_id, &flags); |
| 821 | if (!timr) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | overrun = timr->it_overrun_last; |
| 825 | unlock_timer(timr, flags); |
| 826 | |
| 827 | return overrun; |
| 828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | /* Set a POSIX.1b interval timer. */ |
| 831 | /* timr->it_lock is taken. */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 832 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | common_timer_set(struct k_itimer *timr, int flags, |
| 834 | struct itimerspec *new_setting, struct itimerspec *old_setting) |
| 835 | { |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 836 | struct hrtimer *timer = &timr->it.real.timer; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 837 | enum hrtimer_mode mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
| 839 | if (old_setting) |
| 840 | common_timer_get(timr, old_setting); |
| 841 | |
| 842 | /* disable the timer */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 843 | timr->it.real.interval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* |
| 845 | * careful here. If smp we could be in the "fire" routine which will |
| 846 | * be spinning as we hold the lock. But this is ONLY an SMP issue. |
| 847 | */ |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 848 | if (hrtimer_try_to_cancel(timer) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return TIMER_RETRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
| 851 | timr->it_requeue_pending = (timr->it_requeue_pending + 2) & |
| 852 | ~REQUEUE_PENDING; |
| 853 | timr->it_overrun_last = 0; |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 854 | |
| 855 | /* switch off the timer when it_value is zero */ |
| 856 | if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 859 | mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 860 | hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 861 | timr->it.real.timer.function = posix_timer_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 863 | hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value)); |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 864 | |
| 865 | /* Convert interval */ |
| 866 | timr->it.real.interval = timespec_to_ktime(new_setting->it_interval); |
| 867 | |
| 868 | /* SIGEV_NONE timers are not queued ! See common_timer_get */ |
Thomas Gleixner | 952bbc8 | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 869 | if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) { |
| 870 | /* Setup correct expiry time for relative timers */ |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 871 | if (mode == HRTIMER_MODE_REL) { |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 872 | hrtimer_add_expires(timer, timer->base->get_time()); |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 873 | } |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 874 | return 0; |
Thomas Gleixner | 952bbc8 | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 875 | } |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 876 | |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 877 | hrtimer_start_expires(timer, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* Set a POSIX.1b interval timer */ |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 882 | SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, |
| 883 | const struct itimerspec __user *, new_setting, |
| 884 | struct itimerspec __user *, old_setting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
| 886 | struct k_itimer *timr; |
| 887 | struct itimerspec new_spec, old_spec; |
| 888 | int error = 0; |
Al Viro | 5ba2533 | 2007-10-14 19:35:50 +0100 | [diff] [blame] | 889 | unsigned long flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | struct itimerspec *rtn = old_setting ? &old_spec : NULL; |
Thomas Gleixner | 27722df | 2011-02-01 13:52:01 +0000 | [diff] [blame] | 891 | struct k_clock *kc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | if (!new_setting) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) |
| 897 | return -EFAULT; |
| 898 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 899 | if (!timespec_valid(&new_spec.it_interval) || |
| 900 | !timespec_valid(&new_spec.it_value)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return -EINVAL; |
| 902 | retry: |
| 903 | timr = lock_timer(timer_id, &flag); |
| 904 | if (!timr) |
| 905 | return -EINVAL; |
| 906 | |
Thomas Gleixner | 27722df | 2011-02-01 13:52:01 +0000 | [diff] [blame] | 907 | kc = clockid_to_kclock(timr->it_clock); |
| 908 | if (WARN_ON_ONCE(!kc || !kc->timer_set)) |
| 909 | error = -EINVAL; |
| 910 | else |
| 911 | error = kc->timer_set(timr, flags, &new_spec, rtn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
| 913 | unlock_timer(timr, flag); |
| 914 | if (error == TIMER_RETRY) { |
| 915 | rtn = NULL; // We already got the old time... |
| 916 | goto retry; |
| 917 | } |
| 918 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 919 | if (old_setting && !error && |
| 920 | copy_to_user(old_setting, &old_spec, sizeof (old_spec))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | error = -EFAULT; |
| 922 | |
| 923 | return error; |
| 924 | } |
| 925 | |
Thomas Gleixner | 6761c67 | 2011-02-01 13:52:07 +0000 | [diff] [blame] | 926 | static int common_timer_del(struct k_itimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 928 | timer->it.real.interval = 0; |
Oleg Nesterov | f972be3 | 2005-06-23 00:09:00 -0700 | [diff] [blame] | 929 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 930 | if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return TIMER_RETRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static inline int timer_delete_hook(struct k_itimer *timer) |
| 936 | { |
Thomas Gleixner | 6761c67 | 2011-02-01 13:52:07 +0000 | [diff] [blame] | 937 | struct k_clock *kc = clockid_to_kclock(timer->it_clock); |
| 938 | |
| 939 | if (WARN_ON_ONCE(!kc || !kc->timer_del)) |
| 940 | return -EINVAL; |
| 941 | return kc->timer_del(timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Delete a POSIX.1b interval timer. */ |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 945 | SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
| 947 | struct k_itimer *timer; |
Al Viro | 5ba2533 | 2007-10-14 19:35:50 +0100 | [diff] [blame] | 948 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | retry_delete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | timer = lock_timer(timer_id, &flags); |
| 952 | if (!timer) |
| 953 | return -EINVAL; |
| 954 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 955 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | unlock_timer(timer, flags); |
| 957 | goto retry_delete; |
| 958 | } |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | spin_lock(¤t->sighand->siglock); |
| 961 | list_del(&timer->list); |
| 962 | spin_unlock(¤t->sighand->siglock); |
| 963 | /* |
| 964 | * This keeps any tasks waiting on the spin lock from thinking |
| 965 | * they got something (see the lock code above). |
| 966 | */ |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 967 | timer->it_signal = NULL; |
Oleg Nesterov | 4b7a130 | 2008-07-25 01:47:26 -0700 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | unlock_timer(timer, flags); |
| 970 | release_posix_timer(timer, IT_ID_SET); |
| 971 | return 0; |
| 972 | } |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /* |
| 975 | * return timer owned by the process, used by exit_itimers |
| 976 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 977 | static void itimer_delete(struct k_itimer *timer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | { |
| 979 | unsigned long flags; |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | retry_delete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | spin_lock_irqsave(&timer->it_lock, flags); |
| 983 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 984 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | unlock_timer(timer, flags); |
| 986 | goto retry_delete; |
| 987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | list_del(&timer->list); |
| 989 | /* |
| 990 | * This keeps any tasks waiting on the spin lock from thinking |
| 991 | * they got something (see the lock code above). |
| 992 | */ |
Oleg Nesterov | 8999210 | 2008-12-01 14:18:15 -0800 | [diff] [blame] | 993 | timer->it_signal = NULL; |
Oleg Nesterov | 4b7a130 | 2008-07-25 01:47:26 -0700 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | unlock_timer(timer, flags); |
| 996 | release_posix_timer(timer, IT_ID_SET); |
| 997 | } |
| 998 | |
| 999 | /* |
Roland McGrath | 25f407f | 2005-10-21 15:03:29 -0700 | [diff] [blame] | 1000 | * This is called by do_exit or de_thread, only when there are no more |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | * references to the shared signal_struct. |
| 1002 | */ |
| 1003 | void exit_itimers(struct signal_struct *sig) |
| 1004 | { |
| 1005 | struct k_itimer *tmr; |
| 1006 | |
| 1007 | while (!list_empty(&sig->posix_timers)) { |
| 1008 | tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); |
| 1009 | itimer_delete(tmr); |
| 1010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 1013 | SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, |
| 1014 | const struct timespec __user *, tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | { |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 1016 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame^] | 1017 | struct timespec64 new_tp64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | struct timespec new_tp; |
| 1019 | |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 1020 | if (!kc || !kc->clock_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | return -EINVAL; |
Thomas Gleixner | 26f9a47 | 2011-02-01 13:51:48 +0000 | [diff] [blame] | 1022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (copy_from_user(&new_tp, tp, sizeof (*tp))) |
| 1024 | return -EFAULT; |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame^] | 1025 | new_tp64 = timespec_to_timespec64(new_tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
Deepa Dinamani | 0fe6afe | 2017-03-26 12:04:16 -0700 | [diff] [blame^] | 1027 | return kc->clock_set(which_clock, &new_tp64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 1030 | SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, |
| 1031 | struct timespec __user *,tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | { |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 1033 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 1034 | struct timespec64 kernel_tp64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | struct timespec kernel_tp; |
| 1036 | int error; |
| 1037 | |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 1038 | if (!kc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | return -EINVAL; |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 1040 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 1041 | error = kc->clock_get(which_clock, &kernel_tp64); |
| 1042 | kernel_tp = timespec64_to_timespec(kernel_tp64); |
Thomas Gleixner | 4228577 | 2011-02-01 13:51:50 +0000 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp))) |
| 1045 | error = -EFAULT; |
| 1046 | |
| 1047 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Richard Cochran | f1f1d5e | 2011-02-01 13:52:26 +0000 | [diff] [blame] | 1050 | SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, |
| 1051 | struct timex __user *, utx) |
| 1052 | { |
| 1053 | struct k_clock *kc = clockid_to_kclock(which_clock); |
| 1054 | struct timex ktx; |
| 1055 | int err; |
| 1056 | |
| 1057 | if (!kc) |
| 1058 | return -EINVAL; |
| 1059 | if (!kc->clock_adj) |
| 1060 | return -EOPNOTSUPP; |
| 1061 | |
| 1062 | if (copy_from_user(&ktx, utx, sizeof(ktx))) |
| 1063 | return -EFAULT; |
| 1064 | |
| 1065 | err = kc->clock_adj(which_clock, &ktx); |
| 1066 | |
Miroslav Lichvar | f0dbe81 | 2013-01-11 11:58:58 +0100 | [diff] [blame] | 1067 | if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) |
Richard Cochran | f1f1d5e | 2011-02-01 13:52:26 +0000 | [diff] [blame] | 1068 | return -EFAULT; |
| 1069 | |
| 1070 | return err; |
| 1071 | } |
| 1072 | |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 1073 | SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, |
| 1074 | struct timespec __user *, tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
Thomas Gleixner | e5e542e | 2011-02-01 13:51:53 +0000 | [diff] [blame] | 1076 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 1077 | struct timespec64 rtn_tp64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | struct timespec rtn_tp; |
| 1079 | int error; |
| 1080 | |
Thomas Gleixner | e5e542e | 2011-02-01 13:51:53 +0000 | [diff] [blame] | 1081 | if (!kc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | |
Deepa Dinamani | d2e3e0c | 2017-03-26 12:04:15 -0700 | [diff] [blame] | 1084 | error = kc->clock_getres(which_clock, &rtn_tp64); |
| 1085 | rtn_tp = timespec64_to_timespec(rtn_tp64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Thomas Gleixner | e5e542e | 2011-02-01 13:51:53 +0000 | [diff] [blame] | 1087 | if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | error = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | return error; |
| 1091 | } |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1094 | * nanosleep for monotonic and realtime clocks |
| 1095 | */ |
| 1096 | static int common_nsleep(const clockid_t which_clock, int flags, |
| 1097 | struct timespec *tsave, struct timespec __user *rmtp) |
| 1098 | { |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1099 | return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ? |
| 1100 | HRTIMER_MODE_ABS : HRTIMER_MODE_REL, |
| 1101 | which_clock); |
Thomas Gleixner | 97735f2 | 2006-01-09 20:52:37 -0800 | [diff] [blame] | 1102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
Heiko Carstens | 362e9c0 | 2009-01-14 14:14:07 +0100 | [diff] [blame] | 1104 | SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, |
| 1105 | const struct timespec __user *, rqtp, |
| 1106 | struct timespec __user *, rmtp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | { |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 1108 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | struct timespec t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 1111 | if (!kc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | return -EINVAL; |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 1113 | if (!kc->nsleep) |
| 1114 | return -ENANOSLEEP_NOTSUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (copy_from_user(&t, rqtp, sizeof (struct timespec))) |
| 1117 | return -EFAULT; |
| 1118 | |
Thomas Gleixner | 5f82b2b | 2006-01-09 20:52:29 -0800 | [diff] [blame] | 1119 | if (!timespec_valid(&t)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | return -EINVAL; |
| 1121 | |
Thomas Gleixner | a5cd288 | 2011-02-01 13:51:11 +0000 | [diff] [blame] | 1122 | return kc->nsleep(which_clock, flags, &t, rmtp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1124 | |
| 1125 | /* |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1126 | * This will restart clock_nanosleep. This is required only by |
| 1127 | * compat_clock_nanosleep_restart for now. |
| 1128 | */ |
Thomas Gleixner | 59bd5bc | 2011-02-01 13:51:17 +0000 | [diff] [blame] | 1129 | long clock_nanosleep_restart(struct restart_block *restart_block) |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1130 | { |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1131 | clockid_t which_clock = restart_block->nanosleep.clockid; |
Thomas Gleixner | 59bd5bc | 2011-02-01 13:51:17 +0000 | [diff] [blame] | 1132 | struct k_clock *kc = clockid_to_kclock(which_clock); |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1133 | |
Thomas Gleixner | 59bd5bc | 2011-02-01 13:51:17 +0000 | [diff] [blame] | 1134 | if (WARN_ON_ONCE(!kc || !kc->nsleep_restart)) |
| 1135 | return -EINVAL; |
| 1136 | |
| 1137 | return kc->nsleep_restart(restart_block); |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1138 | } |