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