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