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