]>
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> |
edbeda46 | 52 | #include <linux/compat.h> |
1da177e4 | 53 | |
8b094cd0 | 54 | #include "timekeeping.h" |
bab0aae9 | 55 | #include "posix-timers.h" |
8b094cd0 | 56 | |
1da177e4 | 57 | /* |
5ed67f05 PE |
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. | |
1da177e4 LT |
64 | */ |
65 | ||
66 | /* | |
67 | * Lets keep our timers in a slab cache :-) | |
68 | */ | |
e18b890b | 69 | static struct kmem_cache *posix_timers_cache; |
5ed67f05 PE |
70 | |
71 | static DEFINE_HASHTABLE(posix_timers_hashtable, 9); | |
72 | static DEFINE_SPINLOCK(hash_lock); | |
1da177e4 | 73 | |
6631fa12 TG |
74 | static const struct k_clock * const posix_clocks[]; |
75 | static const struct k_clock *clockid_to_kclock(const clockid_t id); | |
67edab48 | 76 | static const struct k_clock clock_realtime, clock_monotonic; |
6631fa12 | 77 | |
1da177e4 LT |
78 | /* |
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 | ||
65da528d TG |
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 | |
1da177e4 LT |
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 | |
0061748d | 108 | * clocks. |
1da177e4 LT |
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 | * | |
0061748d RC |
118 | * FUNCTIONS: The CLOCKs structure defines possible functions to |
119 | * handle various clock functions. | |
1da177e4 | 120 | * |
0061748d RC |
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. | |
1da177e4 LT |
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 | */ | |
20f33a03 NK |
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 | }) | |
1da177e4 | 140 | |
5ed67f05 PE |
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 | { | |
5ed67f05 PE |
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 | ||
1da177e4 LT |
191 | static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) |
192 | { | |
193 | spin_unlock_irqrestore(&timr->it_lock, flags); | |
194 | } | |
195 | ||
42285777 | 196 | /* Get clock_realtime */ |
3c9c12f4 | 197 | static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp) |
42285777 | 198 | { |
3c9c12f4 | 199 | ktime_get_real_ts64(tp); |
42285777 TG |
200 | return 0; |
201 | } | |
202 | ||
26f9a479 TG |
203 | /* Set clock_realtime */ |
204 | static int posix_clock_realtime_set(const clockid_t which_clock, | |
0fe6afe3 | 205 | const struct timespec64 *tp) |
26f9a479 | 206 | { |
0fe6afe3 | 207 | return do_sys_settimeofday64(tp, NULL); |
26f9a479 TG |
208 | } |
209 | ||
f1f1d5eb RC |
210 | static int posix_clock_realtime_adj(const clockid_t which_clock, |
211 | struct timex *t) | |
212 | { | |
213 | return do_adjtimex(t); | |
214 | } | |
215 | ||
becf8b5d TG |
216 | /* |
217 | * Get monotonic time for posix timers | |
218 | */ | |
3c9c12f4 | 219 | static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) |
becf8b5d | 220 | { |
3c9c12f4 | 221 | ktime_get_ts64(tp); |
becf8b5d TG |
222 | return 0; |
223 | } | |
1da177e4 | 224 | |
2d42244a | 225 | /* |
7fdd7f89 | 226 | * Get monotonic-raw time for posix timers |
2d42244a | 227 | */ |
3c9c12f4 | 228 | static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) |
2d42244a | 229 | { |
3c9c12f4 | 230 | getrawmonotonic64(tp); |
2d42244a JS |
231 | return 0; |
232 | } | |
233 | ||
da15cfda | 234 | |
3c9c12f4 | 235 | static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp) |
da15cfda | 236 | { |
3c9c12f4 | 237 | *tp = current_kernel_time64(); |
da15cfda JS |
238 | return 0; |
239 | } | |
240 | ||
241 | static int posix_get_monotonic_coarse(clockid_t which_clock, | |
3c9c12f4 | 242 | struct timespec64 *tp) |
da15cfda | 243 | { |
3c9c12f4 | 244 | *tp = get_monotonic_coarse64(); |
da15cfda JS |
245 | return 0; |
246 | } | |
247 | ||
d2e3e0ca | 248 | static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp) |
da15cfda | 249 | { |
d2e3e0ca | 250 | *tp = ktime_to_timespec64(KTIME_LOW_RES); |
da15cfda JS |
251 | return 0; |
252 | } | |
7fdd7f89 | 253 | |
3c9c12f4 | 254 | static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp) |
7fdd7f89 | 255 | { |
3c9c12f4 | 256 | get_monotonic_boottime64(tp); |
7fdd7f89 JS |
257 | return 0; |
258 | } | |
259 | ||
3c9c12f4 | 260 | static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp) |
1ff3c967 | 261 | { |
3c9c12f4 | 262 | timekeeping_clocktai64(tp); |
1ff3c967 JS |
263 | return 0; |
264 | } | |
7fdd7f89 | 265 | |
d2e3e0ca | 266 | static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp) |
056a3cac TG |
267 | { |
268 | tp->tv_sec = 0; | |
269 | tp->tv_nsec = hrtimer_resolution; | |
270 | return 0; | |
271 | } | |
272 | ||
1da177e4 LT |
273 | /* |
274 | * Initialize everything, well, just everything in Posix clocks/timers ;) | |
275 | */ | |
276 | static __init int init_posix_timers(void) | |
277 | { | |
1da177e4 | 278 | posix_timers_cache = kmem_cache_create("posix_timers_cache", |
040b5c6f AD |
279 | sizeof (struct k_itimer), 0, SLAB_PANIC, |
280 | NULL); | |
1da177e4 LT |
281 | return 0; |
282 | } | |
1da177e4 LT |
283 | __initcall(init_posix_timers); |
284 | ||
f37fb0aa | 285 | static void common_hrtimer_rearm(struct k_itimer *timr) |
1da177e4 | 286 | { |
44f21475 RZ |
287 | struct hrtimer *timer = &timr->it.real.timer; |
288 | ||
80105cd0 | 289 | if (!timr->it_interval) |
1da177e4 LT |
290 | return; |
291 | ||
4d672e7a DL |
292 | timr->it_overrun += (unsigned int) hrtimer_forward(timer, |
293 | timer->base->get_time(), | |
80105cd0 | 294 | timr->it_interval); |
44f21475 | 295 | hrtimer_restart(timer); |
1da177e4 LT |
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 | * | |
25985edc | 306 | * To protect against the timer going away while the interrupt is queued, |
1da177e4 LT |
307 | * we require that the it_requeue_pending flag be set. |
308 | */ | |
96fe3b07 | 309 | void posixtimer_rearm(struct siginfo *info) |
1da177e4 LT |
310 | { |
311 | struct k_itimer *timr; | |
312 | unsigned long flags; | |
313 | ||
314 | timr = lock_timer(info->si_tid, &flags); | |
af888d67 TG |
315 | if (!timr) |
316 | return; | |
1da177e4 | 317 | |
af888d67 | 318 | if (timr->it_requeue_pending == info->si_sys_private) { |
f37fb0aa | 319 | timr->kclock->timer_rearm(timr); |
1da177e4 | 320 | |
21e55c1f | 321 | timr->it_active = 1; |
af888d67 TG |
322 | timr->it_overrun_last = timr->it_overrun; |
323 | timr->it_overrun = -1; | |
324 | ++timr->it_requeue_pending; | |
325 | ||
54da1174 | 326 | info->si_overrun += timr->it_overrun_last; |
becf8b5d TG |
327 | } |
328 | ||
af888d67 | 329 | unlock_timer(timr, flags); |
1da177e4 LT |
330 | } |
331 | ||
ba661292 | 332 | int posix_timer_event(struct k_itimer *timr, int si_private) |
1da177e4 | 333 | { |
27af4245 ON |
334 | struct task_struct *task; |
335 | int shared, ret = -1; | |
ba661292 ON |
336 | /* |
337 | * FIXME: if ->sigq is queued we can race with | |
96fe3b07 | 338 | * dequeue_signal()->posixtimer_rearm(). |
ba661292 ON |
339 | * |
340 | * If dequeue_signal() sees the "right" value of | |
96fe3b07 | 341 | * si_sys_private it calls posixtimer_rearm(). |
ba661292 | 342 | * We re-queue ->sigq and drop ->it_lock(). |
96fe3b07 | 343 | * posixtimer_rearm() locks the timer |
ba661292 ON |
344 | * and re-schedules it while ->sigq is pending. |
345 | * Not really bad, but not that we want. | |
346 | */ | |
1da177e4 | 347 | timr->sigq->info.si_sys_private = si_private; |
1da177e4 | 348 | |
27af4245 ON |
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(); | |
4aa73611 ON |
356 | /* If we failed to send the signal the timer stops. */ |
357 | return ret > 0; | |
1da177e4 | 358 | } |
1da177e4 LT |
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 | */ | |
c9cb2e3d | 367 | static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) |
1da177e4 | 368 | { |
05cfb614 | 369 | struct k_itimer *timr; |
1da177e4 | 370 | unsigned long flags; |
becf8b5d | 371 | int si_private = 0; |
c9cb2e3d | 372 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
1da177e4 | 373 | |
05cfb614 | 374 | timr = container_of(timer, struct k_itimer, it.real.timer); |
1da177e4 | 375 | spin_lock_irqsave(&timr->it_lock, flags); |
1da177e4 | 376 | |
21e55c1f | 377 | timr->it_active = 0; |
80105cd0 | 378 | if (timr->it_interval != 0) |
becf8b5d | 379 | si_private = ++timr->it_requeue_pending; |
1da177e4 | 380 | |
becf8b5d TG |
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 | */ | |
80105cd0 | 387 | if (timr->it_interval != 0) { |
58229a18 TG |
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 | { | |
8b0e1953 | 414 | ktime_t kj = NSEC_PER_SEC / HZ; |
58229a18 | 415 | |
80105cd0 | 416 | if (timr->it_interval < kj) |
58229a18 TG |
417 | now = ktime_add(now, kj); |
418 | } | |
419 | #endif | |
4d672e7a | 420 | timr->it_overrun += (unsigned int) |
58229a18 | 421 | hrtimer_forward(timer, now, |
80105cd0 | 422 | timr->it_interval); |
becf8b5d | 423 | ret = HRTIMER_RESTART; |
a0a0c28c | 424 | ++timr->it_requeue_pending; |
21e55c1f | 425 | timr->it_active = 1; |
1da177e4 | 426 | } |
1da177e4 | 427 | } |
1da177e4 | 428 | |
becf8b5d TG |
429 | unlock_timer(timr, flags); |
430 | return ret; | |
431 | } | |
1da177e4 | 432 | |
27af4245 | 433 | static struct pid *good_sigevent(sigevent_t * event) |
1da177e4 LT |
434 | { |
435 | struct task_struct *rtn = current->group_leader; | |
436 | ||
437 | if ((event->sigev_notify & SIGEV_THREAD_ID ) && | |
8dc86af0 | 438 | (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) || |
bac0abd6 | 439 | !same_thread_group(rtn, current) || |
1da177e4 LT |
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 | ||
27af4245 | 447 | return task_pid(rtn); |
1da177e4 LT |
448 | } |
449 | ||
1da177e4 LT |
450 | static struct k_itimer * alloc_posix_timer(void) |
451 | { | |
452 | struct k_itimer *tmr; | |
c3762229 | 453 | tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); |
1da177e4 LT |
454 | if (!tmr) |
455 | return tmr; | |
1da177e4 LT |
456 | if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { |
457 | kmem_cache_free(posix_timers_cache, tmr); | |
aa94fbd5 | 458 | return NULL; |
1da177e4 | 459 | } |
ba661292 | 460 | memset(&tmr->sigq->info, 0, sizeof(siginfo_t)); |
1da177e4 LT |
461 | return tmr; |
462 | } | |
463 | ||
8af08871 ED |
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 | ||
1da177e4 LT |
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; | |
5ed67f05 PE |
477 | spin_lock_irqsave(&hash_lock, flags); |
478 | hlist_del_rcu(&tmr->t_hash); | |
479 | spin_unlock_irqrestore(&hash_lock, flags); | |
1da177e4 | 480 | } |
89992102 | 481 | put_pid(tmr->it_pid); |
1da177e4 | 482 | sigqueue_free(tmr->sigq); |
8af08871 | 483 | call_rcu(&tmr->it.rcu, k_itimer_rcu_free); |
1da177e4 LT |
484 | } |
485 | ||
838394fb TG |
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 | ||
1da177e4 | 492 | /* Create a POSIX.1b interval timer. */ |
2482097c AV |
493 | static int do_timer_create(clockid_t which_clock, struct sigevent *event, |
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 | int it_id_set = IT_ID_NOT_SET; |
500 | ||
838394fb | 501 | if (!kc) |
1da177e4 | 502 | return -EINVAL; |
838394fb TG |
503 | if (!kc->timer_create) |
504 | return -EOPNOTSUPP; | |
1da177e4 LT |
505 | |
506 | new_timer = alloc_posix_timer(); | |
507 | if (unlikely(!new_timer)) | |
508 | return -EAGAIN; | |
509 | ||
510 | spin_lock_init(&new_timer->it_lock); | |
5ed67f05 PE |
511 | new_timer_id = posix_timer_add(new_timer); |
512 | if (new_timer_id < 0) { | |
513 | error = new_timer_id; | |
1da177e4 LT |
514 | goto out; |
515 | } | |
516 | ||
517 | it_id_set = IT_ID_SET; | |
518 | new_timer->it_id = (timer_t) new_timer_id; | |
519 | new_timer->it_clock = which_clock; | |
d97bb75d | 520 | new_timer->kclock = kc; |
1da177e4 | 521 | new_timer->it_overrun = -1; |
1da177e4 | 522 | |
2482097c | 523 | if (event) { |
36b2f046 | 524 | rcu_read_lock(); |
2482097c | 525 | new_timer->it_pid = get_pid(good_sigevent(event)); |
36b2f046 | 526 | rcu_read_unlock(); |
89992102 | 527 | if (!new_timer->it_pid) { |
1da177e4 LT |
528 | error = -EINVAL; |
529 | goto out; | |
530 | } | |
2482097c AV |
531 | new_timer->it_sigev_notify = event->sigev_notify; |
532 | new_timer->sigq->info.si_signo = event->sigev_signo; | |
533 | new_timer->sigq->info.si_value = event->sigev_value; | |
1da177e4 | 534 | } else { |
2482097c AV |
535 | new_timer->it_sigev_notify = SIGEV_SIGNAL; |
536 | new_timer->sigq->info.si_signo = SIGALRM; | |
537 | memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); | |
538 | new_timer->sigq->info.si_value.sival_int = new_timer->it_id; | |
89992102 | 539 | new_timer->it_pid = get_pid(task_tgid(current)); |
1da177e4 LT |
540 | } |
541 | ||
717835d9 | 542 | new_timer->sigq->info.si_tid = new_timer->it_id; |
5a9fa730 | 543 | new_timer->sigq->info.si_code = SI_TIMER; |
717835d9 | 544 | |
2b08de00 AV |
545 | if (copy_to_user(created_timer_id, |
546 | &new_timer_id, sizeof (new_timer_id))) { | |
547 | error = -EFAULT; | |
548 | goto out; | |
549 | } | |
550 | ||
838394fb | 551 | error = kc->timer_create(new_timer); |
45e0fffc AV |
552 | if (error) |
553 | goto out; | |
554 | ||
36b2f046 | 555 | spin_lock_irq(¤t->sighand->siglock); |
27af4245 | 556 | new_timer->it_signal = current->signal; |
36b2f046 ON |
557 | list_add(&new_timer->list, ¤t->signal->posix_timers); |
558 | spin_unlock_irq(¤t->sighand->siglock); | |
ef864c95 ON |
559 | |
560 | return 0; | |
838394fb | 561 | /* |
1da177e4 LT |
562 | * In the case of the timer belonging to another task, after |
563 | * the task is unlocked, the timer is owned by the other task | |
564 | * and may cease to exist at any time. Don't use or modify | |
565 | * new_timer after the unlock call. | |
566 | */ | |
1da177e4 | 567 | out: |
ef864c95 | 568 | release_posix_timer(new_timer, it_id_set); |
1da177e4 LT |
569 | return error; |
570 | } | |
571 | ||
2482097c AV |
572 | SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock, |
573 | struct sigevent __user *, timer_event_spec, | |
574 | timer_t __user *, created_timer_id) | |
575 | { | |
576 | if (timer_event_spec) { | |
577 | sigevent_t event; | |
578 | ||
579 | if (copy_from_user(&event, timer_event_spec, sizeof (event))) | |
580 | return -EFAULT; | |
581 | return do_timer_create(which_clock, &event, created_timer_id); | |
582 | } | |
583 | return do_timer_create(which_clock, NULL, created_timer_id); | |
584 | } | |
585 | ||
586 | #ifdef CONFIG_COMPAT | |
587 | COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock, | |
588 | struct compat_sigevent __user *, timer_event_spec, | |
589 | timer_t __user *, created_timer_id) | |
590 | { | |
591 | if (timer_event_spec) { | |
592 | sigevent_t event; | |
593 | ||
594 | if (get_compat_sigevent(&event, timer_event_spec)) | |
595 | return -EFAULT; | |
596 | return do_timer_create(which_clock, &event, created_timer_id); | |
597 | } | |
598 | return do_timer_create(which_clock, NULL, created_timer_id); | |
599 | } | |
600 | #endif | |
601 | ||
1da177e4 LT |
602 | /* |
603 | * Locking issues: We need to protect the result of the id look up until | |
604 | * we get the timer locked down so it is not deleted under us. The | |
605 | * removal is done under the idr spinlock so we use that here to bridge | |
606 | * the find to the timer lock. To avoid a dead lock, the timer id MUST | |
607 | * be release with out holding the timer lock. | |
608 | */ | |
20f33a03 | 609 | static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags) |
1da177e4 LT |
610 | { |
611 | struct k_itimer *timr; | |
8af08871 | 612 | |
e182bb38 TH |
613 | /* |
614 | * timer_t could be any type >= int and we want to make sure any | |
615 | * @timer_id outside positive int range fails lookup. | |
616 | */ | |
617 | if ((unsigned long long)timer_id > INT_MAX) | |
618 | return NULL; | |
619 | ||
8af08871 | 620 | rcu_read_lock(); |
5ed67f05 | 621 | timr = posix_timer_by_id(timer_id); |
1da177e4 | 622 | if (timr) { |
8af08871 | 623 | spin_lock_irqsave(&timr->it_lock, *flags); |
89992102 | 624 | if (timr->it_signal == current->signal) { |
8af08871 | 625 | rcu_read_unlock(); |
31d92845 ON |
626 | return timr; |
627 | } | |
8af08871 | 628 | spin_unlock_irqrestore(&timr->it_lock, *flags); |
31d92845 | 629 | } |
8af08871 | 630 | rcu_read_unlock(); |
1da177e4 | 631 | |
31d92845 | 632 | return NULL; |
1da177e4 LT |
633 | } |
634 | ||
91d57bae TG |
635 | static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now) |
636 | { | |
637 | struct hrtimer *timer = &timr->it.real.timer; | |
638 | ||
639 | return __hrtimer_expires_remaining_adjusted(timer, now); | |
640 | } | |
641 | ||
642 | static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now) | |
643 | { | |
644 | struct hrtimer *timer = &timr->it.real.timer; | |
645 | ||
646 | return (int)hrtimer_forward(timer, now, timr->it_interval); | |
647 | } | |
648 | ||
1da177e4 LT |
649 | /* |
650 | * Get the time remaining on a POSIX.1b interval timer. This function | |
651 | * is ALWAYS called with spin_lock_irq on the timer, thus it must not | |
652 | * mess with irq. | |
653 | * | |
654 | * We have a couple of messes to clean up here. First there is the case | |
655 | * of a timer that has a requeue pending. These timers should appear to | |
656 | * be in the timer list with an expiry as if we were to requeue them | |
657 | * now. | |
658 | * | |
659 | * The second issue is the SIGEV_NONE timer which may be active but is | |
660 | * not really ever put in the timer list (to save system resources). | |
661 | * This timer may be expired, and if so, we will do it here. Otherwise | |
662 | * it is the same as a requeue pending timer WRT to what we should | |
663 | * report. | |
664 | */ | |
f2c45807 | 665 | void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting) |
1da177e4 | 666 | { |
91d57bae | 667 | const struct k_clock *kc = timr->kclock; |
3b98a532 | 668 | ktime_t now, remaining, iv; |
91d57bae TG |
669 | struct timespec64 ts64; |
670 | bool sig_none; | |
1da177e4 | 671 | |
c6503be5 | 672 | sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE; |
80105cd0 | 673 | iv = timr->it_interval; |
3b98a532 | 674 | |
becf8b5d | 675 | /* interval timer ? */ |
91d57bae | 676 | if (iv) { |
5f252b32 | 677 | cur_setting->it_interval = ktime_to_timespec64(iv); |
91d57bae TG |
678 | } else if (!timr->it_active) { |
679 | /* | |
680 | * SIGEV_NONE oneshot timers are never queued. Check them | |
681 | * below. | |
682 | */ | |
683 | if (!sig_none) | |
684 | return; | |
685 | } | |
3b98a532 | 686 | |
91d57bae TG |
687 | /* |
688 | * The timespec64 based conversion is suboptimal, but it's not | |
689 | * worth to implement yet another callback. | |
690 | */ | |
691 | kc->clock_get(timr->it_clock, &ts64); | |
692 | now = timespec64_to_ktime(ts64); | |
3b98a532 | 693 | |
becf8b5d | 694 | /* |
91d57bae TG |
695 | * When a requeue is pending or this is a SIGEV_NONE timer move the |
696 | * expiry time forward by intervals, so expiry is > now. | |
becf8b5d | 697 | */ |
91d57bae TG |
698 | if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) |
699 | timr->it_overrun += kc->timer_forward(timr, now); | |
3b98a532 | 700 | |
91d57bae | 701 | remaining = kc->timer_remaining(timr, now); |
becf8b5d | 702 | /* Return 0 only, when the timer is expired and not pending */ |
2456e855 | 703 | if (remaining <= 0) { |
3b98a532 RZ |
704 | /* |
705 | * A single shot SIGEV_NONE timer must return 0, when | |
706 | * it is expired ! | |
707 | */ | |
91d57bae | 708 | if (!sig_none) |
3b98a532 | 709 | cur_setting->it_value.tv_nsec = 1; |
91d57bae | 710 | } else { |
5f252b32 | 711 | cur_setting->it_value = ktime_to_timespec64(remaining); |
91d57bae | 712 | } |
1da177e4 LT |
713 | } |
714 | ||
715 | /* Get the time remaining on a POSIX.1b interval timer. */ | |
b0dc1242 | 716 | static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting) |
1da177e4 | 717 | { |
a7319fa2 | 718 | struct k_itimer *timr; |
d3ba5a9a | 719 | const struct k_clock *kc; |
1da177e4 | 720 | unsigned long flags; |
a7319fa2 | 721 | int ret = 0; |
1da177e4 LT |
722 | |
723 | timr = lock_timer(timer_id, &flags); | |
724 | if (!timr) | |
725 | return -EINVAL; | |
726 | ||
b0dc1242 | 727 | memset(setting, 0, sizeof(*setting)); |
d97bb75d | 728 | kc = timr->kclock; |
a7319fa2 TG |
729 | if (WARN_ON_ONCE(!kc || !kc->timer_get)) |
730 | ret = -EINVAL; | |
731 | else | |
b0dc1242 | 732 | kc->timer_get(timr, setting); |
1da177e4 LT |
733 | |
734 | unlock_timer(timr, flags); | |
b0dc1242 AV |
735 | return ret; |
736 | } | |
1da177e4 | 737 | |
b0dc1242 AV |
738 | /* Get the time remaining on a POSIX.1b interval timer. */ |
739 | SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, | |
740 | struct itimerspec __user *, setting) | |
741 | { | |
742 | struct itimerspec64 cur_setting64; | |
1da177e4 | 743 | |
b0dc1242 AV |
744 | int ret = do_timer_gettime(timer_id, &cur_setting64); |
745 | if (!ret) { | |
746 | struct itimerspec cur_setting; | |
747 | cur_setting = itimerspec64_to_itimerspec(&cur_setting64); | |
748 | if (copy_to_user(setting, &cur_setting, sizeof (cur_setting))) | |
749 | ret = -EFAULT; | |
750 | } | |
a7319fa2 | 751 | return ret; |
1da177e4 | 752 | } |
becf8b5d | 753 | |
b0dc1242 AV |
754 | #ifdef CONFIG_COMPAT |
755 | COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, | |
756 | struct compat_itimerspec __user *, setting) | |
757 | { | |
758 | struct itimerspec64 cur_setting64; | |
759 | ||
760 | int ret = do_timer_gettime(timer_id, &cur_setting64); | |
761 | if (!ret) { | |
762 | struct itimerspec cur_setting; | |
763 | cur_setting = itimerspec64_to_itimerspec(&cur_setting64); | |
764 | if (put_compat_itimerspec(setting, &cur_setting)) | |
765 | ret = -EFAULT; | |
766 | } | |
767 | return ret; | |
768 | } | |
769 | #endif | |
770 | ||
1da177e4 LT |
771 | /* |
772 | * Get the number of overruns of a POSIX.1b interval timer. This is to | |
773 | * be the overrun of the timer last delivered. At the same time we are | |
774 | * accumulating overruns on the next timer. The overrun is frozen when | |
775 | * the signal is delivered, either at the notify time (if the info block | |
776 | * is not queued) or at the actual delivery time (as we are informed by | |
96fe3b07 | 777 | * the call back to posixtimer_rearm(). So all we need to do is |
1da177e4 LT |
778 | * to pick up the frozen overrun. |
779 | */ | |
362e9c07 | 780 | SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) |
1da177e4 LT |
781 | { |
782 | struct k_itimer *timr; | |
783 | int overrun; | |
5ba25331 | 784 | unsigned long flags; |
1da177e4 LT |
785 | |
786 | timr = lock_timer(timer_id, &flags); | |
787 | if (!timr) | |
788 | return -EINVAL; | |
789 | ||
790 | overrun = timr->it_overrun_last; | |
791 | unlock_timer(timr, flags); | |
792 | ||
793 | return overrun; | |
794 | } | |
1da177e4 | 795 | |
eae1c4ae TG |
796 | static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires, |
797 | bool absolute, bool sigev_none) | |
798 | { | |
799 | struct hrtimer *timer = &timr->it.real.timer; | |
800 | enum hrtimer_mode mode; | |
801 | ||
802 | mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; | |
67edab48 TG |
803 | /* |
804 | * Posix magic: Relative CLOCK_REALTIME timers are not affected by | |
805 | * clock modifications, so they become CLOCK_MONOTONIC based under the | |
806 | * hood. See hrtimer_init(). Update timr->kclock, so the generic | |
807 | * functions which use timr->kclock->clock_get() work. | |
808 | * | |
809 | * Note: it_clock stays unmodified, because the next timer_set() might | |
810 | * use ABSTIME, so it needs to switch back. | |
811 | */ | |
812 | if (timr->it_clock == CLOCK_REALTIME) | |
813 | timr->kclock = absolute ? &clock_realtime : &clock_monotonic; | |
814 | ||
eae1c4ae TG |
815 | hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); |
816 | timr->it.real.timer.function = posix_timer_fn; | |
817 | ||
818 | if (!absolute) | |
819 | expires = ktime_add_safe(expires, timer->base->get_time()); | |
820 | hrtimer_set_expires(timer, expires); | |
821 | ||
822 | if (!sigev_none) | |
823 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); | |
824 | } | |
825 | ||
826 | static int common_hrtimer_try_to_cancel(struct k_itimer *timr) | |
827 | { | |
828 | return hrtimer_try_to_cancel(&timr->it.real.timer); | |
829 | } | |
830 | ||
1da177e4 | 831 | /* Set a POSIX.1b interval timer. */ |
f2c45807 TG |
832 | int common_timer_set(struct k_itimer *timr, int flags, |
833 | struct itimerspec64 *new_setting, | |
834 | struct itimerspec64 *old_setting) | |
1da177e4 | 835 | { |
eae1c4ae TG |
836 | const struct k_clock *kc = timr->kclock; |
837 | bool sigev_none; | |
838 | ktime_t expires; | |
1da177e4 LT |
839 | |
840 | if (old_setting) | |
841 | common_timer_get(timr, old_setting); | |
842 | ||
eae1c4ae | 843 | /* Prevent rearming by clearing the interval */ |
80105cd0 | 844 | timr->it_interval = 0; |
1da177e4 | 845 | /* |
eae1c4ae TG |
846 | * Careful here. On SMP systems the timer expiry function could be |
847 | * active and spinning on timr->it_lock. | |
1da177e4 | 848 | */ |
eae1c4ae | 849 | if (kc->timer_try_to_cancel(timr) < 0) |
1da177e4 | 850 | return TIMER_RETRY; |
1da177e4 | 851 | |
21e55c1f TG |
852 | timr->it_active = 0; |
853 | timr->it_requeue_pending = (timr->it_requeue_pending + 2) & | |
1da177e4 LT |
854 | ~REQUEUE_PENDING; |
855 | timr->it_overrun_last = 0; | |
1da177e4 | 856 | |
eae1c4ae | 857 | /* Switch off the timer when it_value is zero */ |
becf8b5d TG |
858 | if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) |
859 | return 0; | |
1da177e4 | 860 | |
80105cd0 | 861 | timr->it_interval = timespec64_to_ktime(new_setting->it_interval); |
eae1c4ae TG |
862 | expires = timespec64_to_ktime(new_setting->it_value); |
863 | sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE; | |
becf8b5d | 864 | |
eae1c4ae TG |
865 | kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); |
866 | timr->it_active = !sigev_none; | |
1da177e4 LT |
867 | return 0; |
868 | } | |
869 | ||
1acbe770 AV |
870 | static int do_timer_settime(timer_t timer_id, int flags, |
871 | struct itimerspec64 *new_spec64, | |
872 | struct itimerspec64 *old_spec64) | |
1da177e4 | 873 | { |
1acbe770 | 874 | const struct k_clock *kc; |
5f252b32 | 875 | struct k_itimer *timr; |
5ba25331 | 876 | unsigned long flag; |
5f252b32 | 877 | int error = 0; |
1da177e4 | 878 | |
1acbe770 AV |
879 | if (!timespec64_valid(&new_spec64->it_interval) || |
880 | !timespec64_valid(&new_spec64->it_value)) | |
1da177e4 LT |
881 | return -EINVAL; |
882 | ||
1acbe770 AV |
883 | if (old_spec64) |
884 | memset(old_spec64, 0, sizeof(*old_spec64)); | |
1da177e4 LT |
885 | retry: |
886 | timr = lock_timer(timer_id, &flag); | |
887 | if (!timr) | |
888 | return -EINVAL; | |
889 | ||
d97bb75d | 890 | kc = timr->kclock; |
27722df1 TG |
891 | if (WARN_ON_ONCE(!kc || !kc->timer_set)) |
892 | error = -EINVAL; | |
893 | else | |
1acbe770 | 894 | error = kc->timer_set(timr, flags, new_spec64, old_spec64); |
1da177e4 LT |
895 | |
896 | unlock_timer(timr, flag); | |
897 | if (error == TIMER_RETRY) { | |
1acbe770 | 898 | old_spec64 = NULL; // We already got the old time... |
1da177e4 LT |
899 | goto retry; |
900 | } | |
901 | ||
1acbe770 AV |
902 | return error; |
903 | } | |
1da177e4 | 904 | |
1acbe770 AV |
905 | /* Set a POSIX.1b interval timer */ |
906 | SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, | |
907 | const struct itimerspec __user *, new_setting, | |
908 | struct itimerspec __user *, old_setting) | |
909 | { | |
910 | struct itimerspec64 new_spec64, old_spec64; | |
911 | struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL; | |
912 | struct itimerspec new_spec; | |
913 | int error = 0; | |
914 | ||
915 | if (!new_setting) | |
916 | return -EINVAL; | |
917 | ||
918 | if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) | |
919 | return -EFAULT; | |
920 | new_spec64 = itimerspec_to_itimerspec64(&new_spec); | |
921 | ||
922 | error = do_timer_settime(timer_id, flags, &new_spec64, rtn); | |
923 | if (!error && old_setting) { | |
924 | struct itimerspec old_spec; | |
925 | old_spec = itimerspec64_to_itimerspec(&old_spec64); | |
926 | if (copy_to_user(old_setting, &old_spec, sizeof (old_spec))) | |
927 | error = -EFAULT; | |
928 | } | |
929 | return error; | |
930 | } | |
931 | ||
932 | #ifdef CONFIG_COMPAT | |
933 | COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, | |
934 | struct compat_itimerspec __user *, new, | |
935 | struct compat_itimerspec __user *, old) | |
936 | { | |
937 | struct itimerspec64 new_spec64, old_spec64; | |
938 | struct itimerspec64 *rtn = old ? &old_spec64 : NULL; | |
939 | struct itimerspec new_spec; | |
940 | int error = 0; | |
941 | ||
942 | if (!new) | |
943 | return -EINVAL; | |
944 | if (get_compat_itimerspec(&new_spec, new)) | |
945 | return -EFAULT; | |
946 | ||
947 | new_spec64 = itimerspec_to_itimerspec64(&new_spec); | |
948 | error = do_timer_settime(timer_id, flags, &new_spec64, rtn); | |
949 | if (!error && old) { | |
950 | struct itimerspec old_spec; | |
951 | old_spec = itimerspec64_to_itimerspec(&old_spec64); | |
952 | if (put_compat_itimerspec(old, &old_spec)) | |
953 | error = -EFAULT; | |
954 | } | |
1da177e4 LT |
955 | return error; |
956 | } | |
1acbe770 | 957 | #endif |
1da177e4 | 958 | |
f2c45807 | 959 | int common_timer_del(struct k_itimer *timer) |
1da177e4 | 960 | { |
eae1c4ae | 961 | const struct k_clock *kc = timer->kclock; |
f972be33 | 962 | |
eae1c4ae TG |
963 | timer->it_interval = 0; |
964 | if (kc->timer_try_to_cancel(timer) < 0) | |
1da177e4 | 965 | return TIMER_RETRY; |
21e55c1f | 966 | timer->it_active = 0; |
1da177e4 LT |
967 | return 0; |
968 | } | |
969 | ||
970 | static inline int timer_delete_hook(struct k_itimer *timer) | |
971 | { | |
d97bb75d | 972 | const struct k_clock *kc = timer->kclock; |
6761c670 TG |
973 | |
974 | if (WARN_ON_ONCE(!kc || !kc->timer_del)) | |
975 | return -EINVAL; | |
976 | return kc->timer_del(timer); | |
1da177e4 LT |
977 | } |
978 | ||
979 | /* Delete a POSIX.1b interval timer. */ | |
362e9c07 | 980 | SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) |
1da177e4 LT |
981 | { |
982 | struct k_itimer *timer; | |
5ba25331 | 983 | unsigned long flags; |
1da177e4 | 984 | |
1da177e4 | 985 | retry_delete: |
1da177e4 LT |
986 | timer = lock_timer(timer_id, &flags); |
987 | if (!timer) | |
988 | return -EINVAL; | |
989 | ||
becf8b5d | 990 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
1da177e4 LT |
991 | unlock_timer(timer, flags); |
992 | goto retry_delete; | |
993 | } | |
becf8b5d | 994 | |
1da177e4 LT |
995 | spin_lock(¤t->sighand->siglock); |
996 | list_del(&timer->list); | |
997 | spin_unlock(¤t->sighand->siglock); | |
998 | /* | |
999 | * This keeps any tasks waiting on the spin lock from thinking | |
1000 | * they got something (see the lock code above). | |
1001 | */ | |
89992102 | 1002 | timer->it_signal = NULL; |
4b7a1304 | 1003 | |
1da177e4 LT |
1004 | unlock_timer(timer, flags); |
1005 | release_posix_timer(timer, IT_ID_SET); | |
1006 | return 0; | |
1007 | } | |
becf8b5d | 1008 | |
1da177e4 LT |
1009 | /* |
1010 | * return timer owned by the process, used by exit_itimers | |
1011 | */ | |
858119e1 | 1012 | static void itimer_delete(struct k_itimer *timer) |
1da177e4 LT |
1013 | { |
1014 | unsigned long flags; | |
1015 | ||
1da177e4 | 1016 | retry_delete: |
1da177e4 LT |
1017 | spin_lock_irqsave(&timer->it_lock, flags); |
1018 | ||
becf8b5d | 1019 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
1da177e4 LT |
1020 | unlock_timer(timer, flags); |
1021 | goto retry_delete; | |
1022 | } | |
1da177e4 LT |
1023 | list_del(&timer->list); |
1024 | /* | |
1025 | * This keeps any tasks waiting on the spin lock from thinking | |
1026 | * they got something (see the lock code above). | |
1027 | */ | |
89992102 | 1028 | timer->it_signal = NULL; |
4b7a1304 | 1029 | |
1da177e4 LT |
1030 | unlock_timer(timer, flags); |
1031 | release_posix_timer(timer, IT_ID_SET); | |
1032 | } | |
1033 | ||
1034 | /* | |
25f407f0 | 1035 | * This is called by do_exit or de_thread, only when there are no more |
1da177e4 LT |
1036 | * references to the shared signal_struct. |
1037 | */ | |
1038 | void exit_itimers(struct signal_struct *sig) | |
1039 | { | |
1040 | struct k_itimer *tmr; | |
1041 | ||
1042 | while (!list_empty(&sig->posix_timers)) { | |
1043 | tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); | |
1044 | itimer_delete(tmr); | |
1045 | } | |
1046 | } | |
1047 | ||
362e9c07 HC |
1048 | SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, |
1049 | const struct timespec __user *, tp) | |
1da177e4 | 1050 | { |
d3ba5a9a | 1051 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
0fe6afe3 | 1052 | struct timespec64 new_tp64; |
1da177e4 LT |
1053 | struct timespec new_tp; |
1054 | ||
26f9a479 | 1055 | if (!kc || !kc->clock_set) |
1da177e4 | 1056 | return -EINVAL; |
26f9a479 | 1057 | |
1da177e4 LT |
1058 | if (copy_from_user(&new_tp, tp, sizeof (*tp))) |
1059 | return -EFAULT; | |
0fe6afe3 | 1060 | new_tp64 = timespec_to_timespec64(new_tp); |
1da177e4 | 1061 | |
0fe6afe3 | 1062 | return kc->clock_set(which_clock, &new_tp64); |
1da177e4 LT |
1063 | } |
1064 | ||
362e9c07 HC |
1065 | SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, |
1066 | struct timespec __user *,tp) | |
1da177e4 | 1067 | { |
d3ba5a9a | 1068 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
3c9c12f4 | 1069 | struct timespec64 kernel_tp64; |
1da177e4 LT |
1070 | struct timespec kernel_tp; |
1071 | int error; | |
1072 | ||
42285777 | 1073 | if (!kc) |
1da177e4 | 1074 | return -EINVAL; |
42285777 | 1075 | |
3c9c12f4 DD |
1076 | error = kc->clock_get(which_clock, &kernel_tp64); |
1077 | kernel_tp = timespec64_to_timespec(kernel_tp64); | |
42285777 | 1078 | |
1da177e4 LT |
1079 | if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp))) |
1080 | error = -EFAULT; | |
1081 | ||
1082 | return error; | |
1da177e4 LT |
1083 | } |
1084 | ||
f1f1d5eb RC |
1085 | SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, |
1086 | struct timex __user *, utx) | |
1087 | { | |
d3ba5a9a | 1088 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
f1f1d5eb RC |
1089 | struct timex ktx; |
1090 | int err; | |
1091 | ||
1092 | if (!kc) | |
1093 | return -EINVAL; | |
1094 | if (!kc->clock_adj) | |
1095 | return -EOPNOTSUPP; | |
1096 | ||
1097 | if (copy_from_user(&ktx, utx, sizeof(ktx))) | |
1098 | return -EFAULT; | |
1099 | ||
1100 | err = kc->clock_adj(which_clock, &ktx); | |
1101 | ||
f0dbe81f | 1102 | if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) |
f1f1d5eb RC |
1103 | return -EFAULT; |
1104 | ||
1105 | return err; | |
1106 | } | |
1107 | ||
d822cdcc AV |
1108 | SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, |
1109 | struct timespec __user *, tp) | |
1110 | { | |
1111 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
1112 | struct timespec64 rtn_tp64; | |
1113 | struct timespec rtn_tp; | |
1114 | int error; | |
1115 | ||
1116 | if (!kc) | |
1117 | return -EINVAL; | |
1118 | ||
1119 | error = kc->clock_getres(which_clock, &rtn_tp64); | |
1120 | rtn_tp = timespec64_to_timespec(rtn_tp64); | |
1121 | ||
1122 | if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) | |
1123 | error = -EFAULT; | |
1124 | ||
1125 | return error; | |
1126 | } | |
1127 | ||
3a4d44b6 AV |
1128 | #ifdef CONFIG_COMPAT |
1129 | ||
d822cdcc AV |
1130 | COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock, |
1131 | struct compat_timespec __user *, tp) | |
1132 | { | |
1133 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
1134 | struct timespec64 new_tp64; | |
1135 | struct timespec new_tp; | |
1136 | ||
1137 | if (!kc || !kc->clock_set) | |
1138 | return -EINVAL; | |
1139 | ||
1140 | if (compat_get_timespec(&new_tp, tp)) | |
1141 | return -EFAULT; | |
1142 | ||
1143 | new_tp64 = timespec_to_timespec64(new_tp); | |
1144 | ||
1145 | return kc->clock_set(which_clock, &new_tp64); | |
1146 | } | |
1147 | ||
1148 | COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, | |
1149 | struct compat_timespec __user *, tp) | |
1150 | { | |
1151 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
1152 | struct timespec64 kernel_tp64; | |
1153 | struct timespec kernel_tp; | |
1154 | int error; | |
1155 | ||
1156 | if (!kc) | |
1157 | return -EINVAL; | |
1158 | ||
1159 | error = kc->clock_get(which_clock, &kernel_tp64); | |
1160 | kernel_tp = timespec64_to_timespec(kernel_tp64); | |
1161 | ||
1162 | if (!error && compat_put_timespec(&kernel_tp, tp)) | |
1163 | error = -EFAULT; | |
1164 | ||
1165 | return error; | |
1166 | } | |
1167 | ||
3a4d44b6 AV |
1168 | COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, |
1169 | struct compat_timex __user *, utp) | |
1170 | { | |
1171 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
1172 | struct timex ktx; | |
1173 | int err; | |
1174 | ||
1175 | if (!kc) | |
1176 | return -EINVAL; | |
1177 | if (!kc->clock_adj) | |
1178 | return -EOPNOTSUPP; | |
1179 | ||
1180 | err = compat_get_timex(&ktx, utp); | |
1181 | if (err) | |
1182 | return err; | |
1183 | ||
1184 | err = kc->clock_adj(which_clock, &ktx); | |
1185 | ||
1186 | if (err >= 0) | |
1187 | err = compat_put_timex(utp, &ktx); | |
1188 | ||
1189 | return err; | |
1190 | } | |
3a4d44b6 | 1191 | |
d822cdcc AV |
1192 | COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock, |
1193 | struct compat_timespec __user *, tp) | |
1da177e4 | 1194 | { |
d3ba5a9a | 1195 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
d2e3e0ca | 1196 | struct timespec64 rtn_tp64; |
1da177e4 LT |
1197 | struct timespec rtn_tp; |
1198 | int error; | |
1199 | ||
e5e542ee | 1200 | if (!kc) |
1da177e4 LT |
1201 | return -EINVAL; |
1202 | ||
d2e3e0ca DD |
1203 | error = kc->clock_getres(which_clock, &rtn_tp64); |
1204 | rtn_tp = timespec64_to_timespec(rtn_tp64); | |
1da177e4 | 1205 | |
d822cdcc | 1206 | if (!error && tp && compat_put_timespec(&rtn_tp, tp)) |
1da177e4 | 1207 | error = -EFAULT; |
1da177e4 LT |
1208 | |
1209 | return error; | |
1210 | } | |
d822cdcc | 1211 | #endif |
1da177e4 | 1212 | |
97735f25 TG |
1213 | /* |
1214 | * nanosleep for monotonic and realtime clocks | |
1215 | */ | |
1216 | static int common_nsleep(const clockid_t which_clock, int flags, | |
99e6c0e6 | 1217 | struct timespec64 *tsave) |
97735f25 | 1218 | { |
192a82f9 | 1219 | return hrtimer_nanosleep(tsave, flags & TIMER_ABSTIME ? |
080344b9 ON |
1220 | HRTIMER_MODE_ABS : HRTIMER_MODE_REL, |
1221 | which_clock); | |
97735f25 | 1222 | } |
1da177e4 | 1223 | |
362e9c07 HC |
1224 | SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, |
1225 | const struct timespec __user *, rqtp, | |
1226 | struct timespec __user *, rmtp) | |
1da177e4 | 1227 | { |
d3ba5a9a | 1228 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
ad196384 | 1229 | struct timespec64 t64; |
1da177e4 | 1230 | struct timespec t; |
1da177e4 | 1231 | |
a5cd2880 | 1232 | if (!kc) |
1da177e4 | 1233 | return -EINVAL; |
a5cd2880 TG |
1234 | if (!kc->nsleep) |
1235 | return -ENANOSLEEP_NOTSUP; | |
1da177e4 LT |
1236 | |
1237 | if (copy_from_user(&t, rqtp, sizeof (struct timespec))) | |
1238 | return -EFAULT; | |
1239 | ||
ad196384 DD |
1240 | t64 = timespec_to_timespec64(t); |
1241 | if (!timespec64_valid(&t64)) | |
1da177e4 | 1242 | return -EINVAL; |
99e6c0e6 AV |
1243 | if (flags & TIMER_ABSTIME) |
1244 | rmtp = NULL; | |
edbeda46 | 1245 | current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; |
99e6c0e6 | 1246 | current->restart_block.nanosleep.rmtp = rmtp; |
1da177e4 | 1247 | |
99e6c0e6 | 1248 | return kc->nsleep(which_clock, flags, &t64); |
1da177e4 | 1249 | } |
1711ef38 | 1250 | |
edbeda46 AV |
1251 | #ifdef CONFIG_COMPAT |
1252 | COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags, | |
1253 | struct compat_timespec __user *, rqtp, | |
1254 | struct compat_timespec __user *, rmtp) | |
1711ef38 | 1255 | { |
d3ba5a9a | 1256 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
edbeda46 AV |
1257 | struct timespec64 t64; |
1258 | struct timespec t; | |
59bd5bc2 | 1259 | |
edbeda46 | 1260 | if (!kc) |
59bd5bc2 | 1261 | return -EINVAL; |
edbeda46 AV |
1262 | if (!kc->nsleep) |
1263 | return -ENANOSLEEP_NOTSUP; | |
1264 | ||
1265 | if (compat_get_timespec(&t, rqtp)) | |
1266 | return -EFAULT; | |
1711ef38 | 1267 | |
edbeda46 AV |
1268 | t64 = timespec_to_timespec64(t); |
1269 | if (!timespec64_valid(&t64)) | |
1270 | return -EINVAL; | |
1271 | if (flags & TIMER_ABSTIME) | |
1272 | rmtp = NULL; | |
1273 | current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; | |
1274 | current->restart_block.nanosleep.compat_rmtp = rmtp; | |
1275 | ||
1276 | return kc->nsleep(which_clock, flags, &t64); | |
1711ef38 | 1277 | } |
edbeda46 | 1278 | #endif |
6631fa12 TG |
1279 | |
1280 | static const struct k_clock clock_realtime = { | |
eae1c4ae TG |
1281 | .clock_getres = posix_get_hrtimer_res, |
1282 | .clock_get = posix_clock_realtime_get, | |
1283 | .clock_set = posix_clock_realtime_set, | |
1284 | .clock_adj = posix_clock_realtime_adj, | |
1285 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1286 | .timer_create = common_timer_create, |
1287 | .timer_set = common_timer_set, | |
1288 | .timer_get = common_timer_get, | |
1289 | .timer_del = common_timer_del, | |
1290 | .timer_rearm = common_hrtimer_rearm, | |
1291 | .timer_forward = common_hrtimer_forward, | |
1292 | .timer_remaining = common_hrtimer_remaining, | |
1293 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1294 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1295 | }; |
1296 | ||
1297 | static const struct k_clock clock_monotonic = { | |
eae1c4ae TG |
1298 | .clock_getres = posix_get_hrtimer_res, |
1299 | .clock_get = posix_ktime_get_ts, | |
1300 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1301 | .timer_create = common_timer_create, |
1302 | .timer_set = common_timer_set, | |
1303 | .timer_get = common_timer_get, | |
1304 | .timer_del = common_timer_del, | |
1305 | .timer_rearm = common_hrtimer_rearm, | |
1306 | .timer_forward = common_hrtimer_forward, | |
1307 | .timer_remaining = common_hrtimer_remaining, | |
1308 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1309 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1310 | }; |
1311 | ||
1312 | static const struct k_clock clock_monotonic_raw = { | |
eae1c4ae TG |
1313 | .clock_getres = posix_get_hrtimer_res, |
1314 | .clock_get = posix_get_monotonic_raw, | |
6631fa12 TG |
1315 | }; |
1316 | ||
1317 | static const struct k_clock clock_realtime_coarse = { | |
eae1c4ae TG |
1318 | .clock_getres = posix_get_coarse_res, |
1319 | .clock_get = posix_get_realtime_coarse, | |
6631fa12 TG |
1320 | }; |
1321 | ||
1322 | static const struct k_clock clock_monotonic_coarse = { | |
eae1c4ae TG |
1323 | .clock_getres = posix_get_coarse_res, |
1324 | .clock_get = posix_get_monotonic_coarse, | |
6631fa12 TG |
1325 | }; |
1326 | ||
1327 | static const struct k_clock clock_tai = { | |
eae1c4ae TG |
1328 | .clock_getres = posix_get_hrtimer_res, |
1329 | .clock_get = posix_get_tai, | |
1330 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1331 | .timer_create = common_timer_create, |
1332 | .timer_set = common_timer_set, | |
1333 | .timer_get = common_timer_get, | |
1334 | .timer_del = common_timer_del, | |
1335 | .timer_rearm = common_hrtimer_rearm, | |
1336 | .timer_forward = common_hrtimer_forward, | |
1337 | .timer_remaining = common_hrtimer_remaining, | |
1338 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1339 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1340 | }; |
1341 | ||
1342 | static const struct k_clock clock_boottime = { | |
eae1c4ae TG |
1343 | .clock_getres = posix_get_hrtimer_res, |
1344 | .clock_get = posix_get_boottime, | |
1345 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1346 | .timer_create = common_timer_create, |
1347 | .timer_set = common_timer_set, | |
1348 | .timer_get = common_timer_get, | |
1349 | .timer_del = common_timer_del, | |
1350 | .timer_rearm = common_hrtimer_rearm, | |
1351 | .timer_forward = common_hrtimer_forward, | |
1352 | .timer_remaining = common_hrtimer_remaining, | |
1353 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1354 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1355 | }; |
1356 | ||
1357 | static const struct k_clock * const posix_clocks[] = { | |
1358 | [CLOCK_REALTIME] = &clock_realtime, | |
1359 | [CLOCK_MONOTONIC] = &clock_monotonic, | |
1360 | [CLOCK_PROCESS_CPUTIME_ID] = &clock_process, | |
1361 | [CLOCK_THREAD_CPUTIME_ID] = &clock_thread, | |
1362 | [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw, | |
1363 | [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse, | |
1364 | [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse, | |
1365 | [CLOCK_BOOTTIME] = &clock_boottime, | |
1366 | [CLOCK_REALTIME_ALARM] = &alarm_clock, | |
1367 | [CLOCK_BOOTTIME_ALARM] = &alarm_clock, | |
1368 | [CLOCK_TAI] = &clock_tai, | |
1369 | }; | |
1370 | ||
1371 | static const struct k_clock *clockid_to_kclock(const clockid_t id) | |
1372 | { | |
1373 | if (id < 0) | |
1374 | return (id & CLOCKFD_MASK) == CLOCKFD ? | |
1375 | &clock_posix_dynamic : &clock_posix_cpu; | |
1376 | ||
1377 | if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id]) | |
1378 | return NULL; | |
1379 | return posix_clocks[id]; | |
1380 | } |