]>
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 [email protected] | |
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 | { | |
725816e8 | 742 | struct itimerspec64 cur_setting; |
1da177e4 | 743 | |
725816e8 | 744 | int ret = do_timer_gettime(timer_id, &cur_setting); |
b0dc1242 | 745 | if (!ret) { |
725816e8 | 746 | if (put_itimerspec64(&cur_setting, setting)) |
b0dc1242 AV |
747 | ret = -EFAULT; |
748 | } | |
a7319fa2 | 749 | return ret; |
1da177e4 | 750 | } |
becf8b5d | 751 | |
b0dc1242 AV |
752 | #ifdef CONFIG_COMPAT |
753 | COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id, | |
754 | struct compat_itimerspec __user *, setting) | |
755 | { | |
725816e8 | 756 | struct itimerspec64 cur_setting; |
b0dc1242 | 757 | |
725816e8 | 758 | int ret = do_timer_gettime(timer_id, &cur_setting); |
b0dc1242 | 759 | if (!ret) { |
725816e8 | 760 | if (put_compat_itimerspec64(&cur_setting, setting)) |
b0dc1242 AV |
761 | ret = -EFAULT; |
762 | } | |
763 | return ret; | |
764 | } | |
765 | #endif | |
766 | ||
1da177e4 LT |
767 | /* |
768 | * Get the number of overruns of a POSIX.1b interval timer. This is to | |
769 | * be the overrun of the timer last delivered. At the same time we are | |
770 | * accumulating overruns on the next timer. The overrun is frozen when | |
771 | * the signal is delivered, either at the notify time (if the info block | |
772 | * is not queued) or at the actual delivery time (as we are informed by | |
96fe3b07 | 773 | * the call back to posixtimer_rearm(). So all we need to do is |
1da177e4 LT |
774 | * to pick up the frozen overrun. |
775 | */ | |
362e9c07 | 776 | SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id) |
1da177e4 LT |
777 | { |
778 | struct k_itimer *timr; | |
779 | int overrun; | |
5ba25331 | 780 | unsigned long flags; |
1da177e4 LT |
781 | |
782 | timr = lock_timer(timer_id, &flags); | |
783 | if (!timr) | |
784 | return -EINVAL; | |
785 | ||
786 | overrun = timr->it_overrun_last; | |
787 | unlock_timer(timr, flags); | |
788 | ||
789 | return overrun; | |
790 | } | |
1da177e4 | 791 | |
eae1c4ae TG |
792 | static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires, |
793 | bool absolute, bool sigev_none) | |
794 | { | |
795 | struct hrtimer *timer = &timr->it.real.timer; | |
796 | enum hrtimer_mode mode; | |
797 | ||
798 | mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL; | |
67edab48 TG |
799 | /* |
800 | * Posix magic: Relative CLOCK_REALTIME timers are not affected by | |
801 | * clock modifications, so they become CLOCK_MONOTONIC based under the | |
802 | * hood. See hrtimer_init(). Update timr->kclock, so the generic | |
803 | * functions which use timr->kclock->clock_get() work. | |
804 | * | |
805 | * Note: it_clock stays unmodified, because the next timer_set() might | |
806 | * use ABSTIME, so it needs to switch back. | |
807 | */ | |
808 | if (timr->it_clock == CLOCK_REALTIME) | |
809 | timr->kclock = absolute ? &clock_realtime : &clock_monotonic; | |
810 | ||
eae1c4ae TG |
811 | hrtimer_init(&timr->it.real.timer, timr->it_clock, mode); |
812 | timr->it.real.timer.function = posix_timer_fn; | |
813 | ||
814 | if (!absolute) | |
815 | expires = ktime_add_safe(expires, timer->base->get_time()); | |
816 | hrtimer_set_expires(timer, expires); | |
817 | ||
818 | if (!sigev_none) | |
819 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS); | |
820 | } | |
821 | ||
822 | static int common_hrtimer_try_to_cancel(struct k_itimer *timr) | |
823 | { | |
824 | return hrtimer_try_to_cancel(&timr->it.real.timer); | |
825 | } | |
826 | ||
1da177e4 | 827 | /* Set a POSIX.1b interval timer. */ |
f2c45807 TG |
828 | int common_timer_set(struct k_itimer *timr, int flags, |
829 | struct itimerspec64 *new_setting, | |
830 | struct itimerspec64 *old_setting) | |
1da177e4 | 831 | { |
eae1c4ae TG |
832 | const struct k_clock *kc = timr->kclock; |
833 | bool sigev_none; | |
834 | ktime_t expires; | |
1da177e4 LT |
835 | |
836 | if (old_setting) | |
837 | common_timer_get(timr, old_setting); | |
838 | ||
eae1c4ae | 839 | /* Prevent rearming by clearing the interval */ |
80105cd0 | 840 | timr->it_interval = 0; |
1da177e4 | 841 | /* |
eae1c4ae TG |
842 | * Careful here. On SMP systems the timer expiry function could be |
843 | * active and spinning on timr->it_lock. | |
1da177e4 | 844 | */ |
eae1c4ae | 845 | if (kc->timer_try_to_cancel(timr) < 0) |
1da177e4 | 846 | return TIMER_RETRY; |
1da177e4 | 847 | |
21e55c1f TG |
848 | timr->it_active = 0; |
849 | timr->it_requeue_pending = (timr->it_requeue_pending + 2) & | |
1da177e4 LT |
850 | ~REQUEUE_PENDING; |
851 | timr->it_overrun_last = 0; | |
1da177e4 | 852 | |
eae1c4ae | 853 | /* Switch off the timer when it_value is zero */ |
becf8b5d TG |
854 | if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) |
855 | return 0; | |
1da177e4 | 856 | |
80105cd0 | 857 | timr->it_interval = timespec64_to_ktime(new_setting->it_interval); |
eae1c4ae TG |
858 | expires = timespec64_to_ktime(new_setting->it_value); |
859 | sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE; | |
becf8b5d | 860 | |
eae1c4ae TG |
861 | kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); |
862 | timr->it_active = !sigev_none; | |
1da177e4 LT |
863 | return 0; |
864 | } | |
865 | ||
1acbe770 AV |
866 | static int do_timer_settime(timer_t timer_id, int flags, |
867 | struct itimerspec64 *new_spec64, | |
868 | struct itimerspec64 *old_spec64) | |
1da177e4 | 869 | { |
1acbe770 | 870 | const struct k_clock *kc; |
5f252b32 | 871 | struct k_itimer *timr; |
5ba25331 | 872 | unsigned long flag; |
5f252b32 | 873 | int error = 0; |
1da177e4 | 874 | |
1acbe770 AV |
875 | if (!timespec64_valid(&new_spec64->it_interval) || |
876 | !timespec64_valid(&new_spec64->it_value)) | |
1da177e4 LT |
877 | return -EINVAL; |
878 | ||
1acbe770 AV |
879 | if (old_spec64) |
880 | memset(old_spec64, 0, sizeof(*old_spec64)); | |
1da177e4 LT |
881 | retry: |
882 | timr = lock_timer(timer_id, &flag); | |
883 | if (!timr) | |
884 | return -EINVAL; | |
885 | ||
d97bb75d | 886 | kc = timr->kclock; |
27722df1 TG |
887 | if (WARN_ON_ONCE(!kc || !kc->timer_set)) |
888 | error = -EINVAL; | |
889 | else | |
1acbe770 | 890 | error = kc->timer_set(timr, flags, new_spec64, old_spec64); |
1da177e4 LT |
891 | |
892 | unlock_timer(timr, flag); | |
893 | if (error == TIMER_RETRY) { | |
1acbe770 | 894 | old_spec64 = NULL; // We already got the old time... |
1da177e4 LT |
895 | goto retry; |
896 | } | |
897 | ||
1acbe770 AV |
898 | return error; |
899 | } | |
1da177e4 | 900 | |
1acbe770 AV |
901 | /* Set a POSIX.1b interval timer */ |
902 | SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, | |
903 | const struct itimerspec __user *, new_setting, | |
904 | struct itimerspec __user *, old_setting) | |
905 | { | |
725816e8 DD |
906 | struct itimerspec64 new_spec, old_spec; |
907 | struct itimerspec64 *rtn = old_setting ? &old_spec : NULL; | |
1acbe770 AV |
908 | int error = 0; |
909 | ||
910 | if (!new_setting) | |
911 | return -EINVAL; | |
912 | ||
725816e8 | 913 | if (get_itimerspec64(&new_spec, new_setting)) |
1acbe770 | 914 | return -EFAULT; |
1acbe770 | 915 | |
725816e8 | 916 | error = do_timer_settime(timer_id, flags, &new_spec, rtn); |
1acbe770 | 917 | if (!error && old_setting) { |
725816e8 | 918 | if (put_itimerspec64(&old_spec, old_setting)) |
1acbe770 AV |
919 | error = -EFAULT; |
920 | } | |
921 | return error; | |
922 | } | |
923 | ||
924 | #ifdef CONFIG_COMPAT | |
925 | COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags, | |
926 | struct compat_itimerspec __user *, new, | |
927 | struct compat_itimerspec __user *, old) | |
928 | { | |
725816e8 DD |
929 | struct itimerspec64 new_spec, old_spec; |
930 | struct itimerspec64 *rtn = old ? &old_spec : NULL; | |
1acbe770 AV |
931 | int error = 0; |
932 | ||
933 | if (!new) | |
934 | return -EINVAL; | |
725816e8 | 935 | if (get_compat_itimerspec64(&new_spec, new)) |
1acbe770 AV |
936 | return -EFAULT; |
937 | ||
725816e8 | 938 | error = do_timer_settime(timer_id, flags, &new_spec, rtn); |
1acbe770 | 939 | if (!error && old) { |
725816e8 | 940 | if (put_compat_itimerspec64(&old_spec, old)) |
1acbe770 AV |
941 | error = -EFAULT; |
942 | } | |
1da177e4 LT |
943 | return error; |
944 | } | |
1acbe770 | 945 | #endif |
1da177e4 | 946 | |
f2c45807 | 947 | int common_timer_del(struct k_itimer *timer) |
1da177e4 | 948 | { |
eae1c4ae | 949 | const struct k_clock *kc = timer->kclock; |
f972be33 | 950 | |
eae1c4ae TG |
951 | timer->it_interval = 0; |
952 | if (kc->timer_try_to_cancel(timer) < 0) | |
1da177e4 | 953 | return TIMER_RETRY; |
21e55c1f | 954 | timer->it_active = 0; |
1da177e4 LT |
955 | return 0; |
956 | } | |
957 | ||
958 | static inline int timer_delete_hook(struct k_itimer *timer) | |
959 | { | |
d97bb75d | 960 | const struct k_clock *kc = timer->kclock; |
6761c670 TG |
961 | |
962 | if (WARN_ON_ONCE(!kc || !kc->timer_del)) | |
963 | return -EINVAL; | |
964 | return kc->timer_del(timer); | |
1da177e4 LT |
965 | } |
966 | ||
967 | /* Delete a POSIX.1b interval timer. */ | |
362e9c07 | 968 | SYSCALL_DEFINE1(timer_delete, timer_t, timer_id) |
1da177e4 LT |
969 | { |
970 | struct k_itimer *timer; | |
5ba25331 | 971 | unsigned long flags; |
1da177e4 | 972 | |
1da177e4 | 973 | retry_delete: |
1da177e4 LT |
974 | timer = lock_timer(timer_id, &flags); |
975 | if (!timer) | |
976 | return -EINVAL; | |
977 | ||
becf8b5d | 978 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
1da177e4 LT |
979 | unlock_timer(timer, flags); |
980 | goto retry_delete; | |
981 | } | |
becf8b5d | 982 | |
1da177e4 LT |
983 | spin_lock(¤t->sighand->siglock); |
984 | list_del(&timer->list); | |
985 | spin_unlock(¤t->sighand->siglock); | |
986 | /* | |
987 | * This keeps any tasks waiting on the spin lock from thinking | |
988 | * they got something (see the lock code above). | |
989 | */ | |
89992102 | 990 | timer->it_signal = NULL; |
4b7a1304 | 991 | |
1da177e4 LT |
992 | unlock_timer(timer, flags); |
993 | release_posix_timer(timer, IT_ID_SET); | |
994 | return 0; | |
995 | } | |
becf8b5d | 996 | |
1da177e4 LT |
997 | /* |
998 | * return timer owned by the process, used by exit_itimers | |
999 | */ | |
858119e1 | 1000 | static void itimer_delete(struct k_itimer *timer) |
1da177e4 LT |
1001 | { |
1002 | unsigned long flags; | |
1003 | ||
1da177e4 | 1004 | retry_delete: |
1da177e4 LT |
1005 | spin_lock_irqsave(&timer->it_lock, flags); |
1006 | ||
becf8b5d | 1007 | if (timer_delete_hook(timer) == TIMER_RETRY) { |
1da177e4 LT |
1008 | unlock_timer(timer, flags); |
1009 | goto retry_delete; | |
1010 | } | |
1da177e4 LT |
1011 | list_del(&timer->list); |
1012 | /* | |
1013 | * This keeps any tasks waiting on the spin lock from thinking | |
1014 | * they got something (see the lock code above). | |
1015 | */ | |
89992102 | 1016 | timer->it_signal = NULL; |
4b7a1304 | 1017 | |
1da177e4 LT |
1018 | unlock_timer(timer, flags); |
1019 | release_posix_timer(timer, IT_ID_SET); | |
1020 | } | |
1021 | ||
1022 | /* | |
25f407f0 | 1023 | * This is called by do_exit or de_thread, only when there are no more |
1da177e4 LT |
1024 | * references to the shared signal_struct. |
1025 | */ | |
1026 | void exit_itimers(struct signal_struct *sig) | |
1027 | { | |
1028 | struct k_itimer *tmr; | |
1029 | ||
1030 | while (!list_empty(&sig->posix_timers)) { | |
1031 | tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); | |
1032 | itimer_delete(tmr); | |
1033 | } | |
1034 | } | |
1035 | ||
362e9c07 HC |
1036 | SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, |
1037 | const struct timespec __user *, tp) | |
1da177e4 | 1038 | { |
d3ba5a9a | 1039 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
5c499410 | 1040 | struct timespec64 new_tp; |
1da177e4 | 1041 | |
26f9a479 | 1042 | if (!kc || !kc->clock_set) |
1da177e4 | 1043 | return -EINVAL; |
26f9a479 | 1044 | |
5c499410 | 1045 | if (get_timespec64(&new_tp, tp)) |
1da177e4 LT |
1046 | return -EFAULT; |
1047 | ||
5c499410 | 1048 | return kc->clock_set(which_clock, &new_tp); |
1da177e4 LT |
1049 | } |
1050 | ||
362e9c07 HC |
1051 | SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, |
1052 | struct timespec __user *,tp) | |
1da177e4 | 1053 | { |
d3ba5a9a | 1054 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
5c499410 | 1055 | struct timespec64 kernel_tp; |
1da177e4 LT |
1056 | int error; |
1057 | ||
42285777 | 1058 | if (!kc) |
1da177e4 | 1059 | return -EINVAL; |
42285777 | 1060 | |
5c499410 | 1061 | error = kc->clock_get(which_clock, &kernel_tp); |
42285777 | 1062 | |
5c499410 | 1063 | if (!error && put_timespec64(&kernel_tp, tp)) |
1da177e4 LT |
1064 | error = -EFAULT; |
1065 | ||
1066 | return error; | |
1da177e4 LT |
1067 | } |
1068 | ||
f1f1d5eb RC |
1069 | SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock, |
1070 | struct timex __user *, utx) | |
1071 | { | |
d3ba5a9a | 1072 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
f1f1d5eb RC |
1073 | struct timex ktx; |
1074 | int err; | |
1075 | ||
1076 | if (!kc) | |
1077 | return -EINVAL; | |
1078 | if (!kc->clock_adj) | |
1079 | return -EOPNOTSUPP; | |
1080 | ||
1081 | if (copy_from_user(&ktx, utx, sizeof(ktx))) | |
1082 | return -EFAULT; | |
1083 | ||
1084 | err = kc->clock_adj(which_clock, &ktx); | |
1085 | ||
f0dbe81f | 1086 | if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx))) |
f1f1d5eb RC |
1087 | return -EFAULT; |
1088 | ||
1089 | return err; | |
1090 | } | |
1091 | ||
d822cdcc AV |
1092 | SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock, |
1093 | struct timespec __user *, tp) | |
1094 | { | |
1095 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
5c499410 | 1096 | struct timespec64 rtn_tp; |
d822cdcc AV |
1097 | int error; |
1098 | ||
1099 | if (!kc) | |
1100 | return -EINVAL; | |
1101 | ||
5c499410 | 1102 | error = kc->clock_getres(which_clock, &rtn_tp); |
d822cdcc | 1103 | |
5c499410 | 1104 | if (!error && tp && put_timespec64(&rtn_tp, tp)) |
d822cdcc AV |
1105 | error = -EFAULT; |
1106 | ||
1107 | return error; | |
1108 | } | |
1109 | ||
3a4d44b6 AV |
1110 | #ifdef CONFIG_COMPAT |
1111 | ||
d822cdcc AV |
1112 | COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock, |
1113 | struct compat_timespec __user *, tp) | |
1114 | { | |
1115 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
5c499410 | 1116 | struct timespec64 ts; |
d822cdcc AV |
1117 | |
1118 | if (!kc || !kc->clock_set) | |
1119 | return -EINVAL; | |
1120 | ||
5c499410 | 1121 | if (compat_get_timespec64(&ts, tp)) |
d822cdcc AV |
1122 | return -EFAULT; |
1123 | ||
5c499410 | 1124 | return kc->clock_set(which_clock, &ts); |
d822cdcc AV |
1125 | } |
1126 | ||
1127 | COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, | |
1128 | struct compat_timespec __user *, tp) | |
1129 | { | |
1130 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
5c499410 DD |
1131 | struct timespec64 ts; |
1132 | int err; | |
d822cdcc AV |
1133 | |
1134 | if (!kc) | |
1135 | return -EINVAL; | |
1136 | ||
5c499410 | 1137 | err = kc->clock_get(which_clock, &ts); |
d822cdcc | 1138 | |
5c499410 DD |
1139 | if (!err && compat_put_timespec64(&ts, tp)) |
1140 | err = -EFAULT; | |
d822cdcc | 1141 | |
5c499410 | 1142 | return err; |
d822cdcc AV |
1143 | } |
1144 | ||
3a4d44b6 AV |
1145 | COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock, |
1146 | struct compat_timex __user *, utp) | |
1147 | { | |
1148 | const struct k_clock *kc = clockid_to_kclock(which_clock); | |
1149 | struct timex ktx; | |
1150 | int err; | |
1151 | ||
1152 | if (!kc) | |
1153 | return -EINVAL; | |
1154 | if (!kc->clock_adj) | |
1155 | return -EOPNOTSUPP; | |
1156 | ||
1157 | err = compat_get_timex(&ktx, utp); | |
1158 | if (err) | |
1159 | return err; | |
1160 | ||
1161 | err = kc->clock_adj(which_clock, &ktx); | |
1162 | ||
1163 | if (err >= 0) | |
1164 | err = compat_put_timex(utp, &ktx); | |
1165 | ||
1166 | return err; | |
1167 | } | |
3a4d44b6 | 1168 | |
d822cdcc AV |
1169 | COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock, |
1170 | struct compat_timespec __user *, tp) | |
1da177e4 | 1171 | { |
d3ba5a9a | 1172 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
5c499410 DD |
1173 | struct timespec64 ts; |
1174 | int err; | |
1da177e4 | 1175 | |
e5e542ee | 1176 | if (!kc) |
1da177e4 LT |
1177 | return -EINVAL; |
1178 | ||
5c499410 DD |
1179 | err = kc->clock_getres(which_clock, &ts); |
1180 | if (!err && tp && compat_put_timespec64(&ts, tp)) | |
1181 | return -EFAULT; | |
1da177e4 | 1182 | |
5c499410 | 1183 | return err; |
1da177e4 | 1184 | } |
5c499410 | 1185 | |
d822cdcc | 1186 | #endif |
1da177e4 | 1187 | |
97735f25 TG |
1188 | /* |
1189 | * nanosleep for monotonic and realtime clocks | |
1190 | */ | |
1191 | static int common_nsleep(const clockid_t which_clock, int flags, | |
938e7cf2 | 1192 | const struct timespec64 *rqtp) |
97735f25 | 1193 | { |
938e7cf2 | 1194 | return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ? |
080344b9 ON |
1195 | HRTIMER_MODE_ABS : HRTIMER_MODE_REL, |
1196 | which_clock); | |
97735f25 | 1197 | } |
1da177e4 | 1198 | |
362e9c07 HC |
1199 | SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags, |
1200 | const struct timespec __user *, rqtp, | |
1201 | struct timespec __user *, rmtp) | |
1da177e4 | 1202 | { |
d3ba5a9a | 1203 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
c0edd7c9 | 1204 | struct timespec64 t; |
1da177e4 | 1205 | |
a5cd2880 | 1206 | if (!kc) |
1da177e4 | 1207 | return -EINVAL; |
a5cd2880 TG |
1208 | if (!kc->nsleep) |
1209 | return -ENANOSLEEP_NOTSUP; | |
1da177e4 | 1210 | |
c0edd7c9 | 1211 | if (get_timespec64(&t, rqtp)) |
1da177e4 LT |
1212 | return -EFAULT; |
1213 | ||
c0edd7c9 | 1214 | if (!timespec64_valid(&t)) |
1da177e4 | 1215 | return -EINVAL; |
99e6c0e6 AV |
1216 | if (flags & TIMER_ABSTIME) |
1217 | rmtp = NULL; | |
edbeda46 | 1218 | current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; |
99e6c0e6 | 1219 | current->restart_block.nanosleep.rmtp = rmtp; |
1da177e4 | 1220 | |
c0edd7c9 | 1221 | return kc->nsleep(which_clock, flags, &t); |
1da177e4 | 1222 | } |
1711ef38 | 1223 | |
edbeda46 AV |
1224 | #ifdef CONFIG_COMPAT |
1225 | COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags, | |
1226 | struct compat_timespec __user *, rqtp, | |
1227 | struct compat_timespec __user *, rmtp) | |
1711ef38 | 1228 | { |
d3ba5a9a | 1229 | const struct k_clock *kc = clockid_to_kclock(which_clock); |
c0edd7c9 | 1230 | struct timespec64 t; |
59bd5bc2 | 1231 | |
edbeda46 | 1232 | if (!kc) |
59bd5bc2 | 1233 | return -EINVAL; |
edbeda46 AV |
1234 | if (!kc->nsleep) |
1235 | return -ENANOSLEEP_NOTSUP; | |
1236 | ||
c0edd7c9 | 1237 | if (compat_get_timespec64(&t, rqtp)) |
edbeda46 | 1238 | return -EFAULT; |
1711ef38 | 1239 | |
c0edd7c9 | 1240 | if (!timespec64_valid(&t)) |
edbeda46 AV |
1241 | return -EINVAL; |
1242 | if (flags & TIMER_ABSTIME) | |
1243 | rmtp = NULL; | |
1244 | current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; | |
1245 | current->restart_block.nanosleep.compat_rmtp = rmtp; | |
1246 | ||
c0edd7c9 | 1247 | return kc->nsleep(which_clock, flags, &t); |
1711ef38 | 1248 | } |
edbeda46 | 1249 | #endif |
6631fa12 TG |
1250 | |
1251 | static const struct k_clock clock_realtime = { | |
eae1c4ae TG |
1252 | .clock_getres = posix_get_hrtimer_res, |
1253 | .clock_get = posix_clock_realtime_get, | |
1254 | .clock_set = posix_clock_realtime_set, | |
1255 | .clock_adj = posix_clock_realtime_adj, | |
1256 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1257 | .timer_create = common_timer_create, |
1258 | .timer_set = common_timer_set, | |
1259 | .timer_get = common_timer_get, | |
1260 | .timer_del = common_timer_del, | |
1261 | .timer_rearm = common_hrtimer_rearm, | |
1262 | .timer_forward = common_hrtimer_forward, | |
1263 | .timer_remaining = common_hrtimer_remaining, | |
1264 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1265 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1266 | }; |
1267 | ||
1268 | static const struct k_clock clock_monotonic = { | |
eae1c4ae TG |
1269 | .clock_getres = posix_get_hrtimer_res, |
1270 | .clock_get = posix_ktime_get_ts, | |
1271 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1272 | .timer_create = common_timer_create, |
1273 | .timer_set = common_timer_set, | |
1274 | .timer_get = common_timer_get, | |
1275 | .timer_del = common_timer_del, | |
1276 | .timer_rearm = common_hrtimer_rearm, | |
1277 | .timer_forward = common_hrtimer_forward, | |
1278 | .timer_remaining = common_hrtimer_remaining, | |
1279 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1280 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1281 | }; |
1282 | ||
1283 | static const struct k_clock clock_monotonic_raw = { | |
eae1c4ae TG |
1284 | .clock_getres = posix_get_hrtimer_res, |
1285 | .clock_get = posix_get_monotonic_raw, | |
6631fa12 TG |
1286 | }; |
1287 | ||
1288 | static const struct k_clock clock_realtime_coarse = { | |
eae1c4ae TG |
1289 | .clock_getres = posix_get_coarse_res, |
1290 | .clock_get = posix_get_realtime_coarse, | |
6631fa12 TG |
1291 | }; |
1292 | ||
1293 | static const struct k_clock clock_monotonic_coarse = { | |
eae1c4ae TG |
1294 | .clock_getres = posix_get_coarse_res, |
1295 | .clock_get = posix_get_monotonic_coarse, | |
6631fa12 TG |
1296 | }; |
1297 | ||
1298 | static const struct k_clock clock_tai = { | |
eae1c4ae TG |
1299 | .clock_getres = posix_get_hrtimer_res, |
1300 | .clock_get = posix_get_tai, | |
1301 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1302 | .timer_create = common_timer_create, |
1303 | .timer_set = common_timer_set, | |
1304 | .timer_get = common_timer_get, | |
1305 | .timer_del = common_timer_del, | |
1306 | .timer_rearm = common_hrtimer_rearm, | |
1307 | .timer_forward = common_hrtimer_forward, | |
1308 | .timer_remaining = common_hrtimer_remaining, | |
1309 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1310 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1311 | }; |
1312 | ||
1313 | static const struct k_clock clock_boottime = { | |
eae1c4ae TG |
1314 | .clock_getres = posix_get_hrtimer_res, |
1315 | .clock_get = posix_get_boottime, | |
1316 | .nsleep = common_nsleep, | |
eae1c4ae TG |
1317 | .timer_create = common_timer_create, |
1318 | .timer_set = common_timer_set, | |
1319 | .timer_get = common_timer_get, | |
1320 | .timer_del = common_timer_del, | |
1321 | .timer_rearm = common_hrtimer_rearm, | |
1322 | .timer_forward = common_hrtimer_forward, | |
1323 | .timer_remaining = common_hrtimer_remaining, | |
1324 | .timer_try_to_cancel = common_hrtimer_try_to_cancel, | |
1325 | .timer_arm = common_hrtimer_arm, | |
6631fa12 TG |
1326 | }; |
1327 | ||
1328 | static const struct k_clock * const posix_clocks[] = { | |
1329 | [CLOCK_REALTIME] = &clock_realtime, | |
1330 | [CLOCK_MONOTONIC] = &clock_monotonic, | |
1331 | [CLOCK_PROCESS_CPUTIME_ID] = &clock_process, | |
1332 | [CLOCK_THREAD_CPUTIME_ID] = &clock_thread, | |
1333 | [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw, | |
1334 | [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse, | |
1335 | [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse, | |
1336 | [CLOCK_BOOTTIME] = &clock_boottime, | |
1337 | [CLOCK_REALTIME_ALARM] = &alarm_clock, | |
1338 | [CLOCK_BOOTTIME_ALARM] = &alarm_clock, | |
1339 | [CLOCK_TAI] = &clock_tai, | |
1340 | }; | |
1341 | ||
1342 | static const struct k_clock *clockid_to_kclock(const clockid_t id) | |
1343 | { | |
1344 | if (id < 0) | |
1345 | return (id & CLOCKFD_MASK) == CLOCKFD ? | |
1346 | &clock_posix_dynamic : &clock_posix_cpu; | |
1347 | ||
1348 | if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id]) | |
1349 | return NULL; | |
1350 | return posix_clocks[id]; | |
1351 | } |