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