]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/posix_timers.c | |
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> | |
34 | #include <linux/smp_lock.h> | |
35 | #include <linux/interrupt.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/time.h> | |
67924be8 | 38 | #include <linux/calc64.h> |
1da177e4 LT |
39 | |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/semaphore.h> | |
42 | #include <linux/list.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/compiler.h> | |
45 | #include <linux/idr.h> | |
46 | #include <linux/posix-timers.h> | |
47 | #include <linux/syscalls.h> | |
48 | #include <linux/wait.h> | |
49 | #include <linux/workqueue.h> | |
50 | #include <linux/module.h> | |
51 | ||
1da177e4 LT |
52 | #define CLOCK_REALTIME_RES TICK_NSEC /* In nano seconds. */ |
53 | ||
54 | static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2) | |
55 | { | |
56 | return (u64)mpy1 * mpy2; | |
57 | } | |
58 | /* | |
59 | * Management arrays for POSIX timers. Timers are kept in slab memory | |
60 | * Timer ids are allocated by an external routine that keeps track of the | |
61 | * id and the timer. The external interface is: | |
62 | * | |
63 | * void *idr_find(struct idr *idp, int id); to find timer_id <id> | |
64 | * int idr_get_new(struct idr *idp, void *ptr); to get a new id and | |
65 | * related it to <ptr> | |
66 | * void idr_remove(struct idr *idp, int id); to release <id> | |
67 | * void idr_init(struct idr *idp); to initialize <idp> | |
68 | * which we supply. | |
69 | * The idr_get_new *may* call slab for more memory so it must not be | |
70 | * called under a spin lock. Likewise idr_remore may release memory | |
71 | * (but it may be ok to do this under a lock...). | |
72 | * idr_find is just a memory look up and is quite fast. A -1 return | |
73 | * indicates that the requested id does not exist. | |
74 | */ | |
75 | ||
76 | /* | |
77 | * Lets keep our timers in a slab cache :-) | |
78 | */ | |
79 | static kmem_cache_t *posix_timers_cache; | |
80 | static struct idr posix_timers_id; | |
81 | static DEFINE_SPINLOCK(idr_lock); | |
82 | ||
1da177e4 LT |
83 | /* |
84 | * we assume that the new SIGEV_THREAD_ID shares no bits with the other | |
85 | * SIGEV values. Here we put out an error if this assumption fails. | |
86 | */ | |
87 | #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ | |
88 | ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD)) | |
89 | #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!" | |
90 | #endif | |
91 | ||
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 | |
127 | * timer. 2.) The list, it_lock, it_clock, it_id and it_process | |
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]; | |
143 | /* | |
144 | * We only have one real clock that can be set so we need only one abs list, | |
145 | * even if we should want to have several clocks with differing resolutions. | |
146 | */ | |
147 | static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list), | |
148 | .lock = SPIN_LOCK_UNLOCKED}; | |
149 | ||
150 | static void posix_timer_fn(unsigned long); | |
151 | static u64 do_posix_clock_monotonic_gettime_parts( | |
152 | struct timespec *tp, struct timespec *mo); | |
153 | int do_posix_clock_monotonic_gettime(struct timespec *tp); | |
a924b04d | 154 | static int do_posix_clock_monotonic_get(const clockid_t, struct timespec *tp); |
1da177e4 LT |
155 | |
156 | static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags); | |
157 | ||
158 | static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) | |
159 | { | |
160 | spin_unlock_irqrestore(&timr->it_lock, flags); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Call the k_clock hook function if non-null, or the default function. | |
165 | */ | |
166 | #define CLOCK_DISPATCH(clock, call, arglist) \ | |
167 | ((clock) < 0 ? posix_cpu_##call arglist : \ | |
168 | (posix_clocks[clock].call != NULL \ | |
169 | ? (*posix_clocks[clock].call) arglist : common_##call arglist)) | |
170 | ||
171 | /* | |
172 | * Default clock hook functions when the struct k_clock passed | |
173 | * to register_posix_clock leaves a function pointer null. | |
174 | * | |
175 | * The function common_CALL is the default implementation for | |
176 | * the function pointer CALL in struct k_clock. | |
177 | */ | |
178 | ||
a924b04d | 179 | static inline int common_clock_getres(const clockid_t which_clock, |
1da177e4 LT |
180 | struct timespec *tp) |
181 | { | |
182 | tp->tv_sec = 0; | |
183 | tp->tv_nsec = posix_clocks[which_clock].res; | |
184 | return 0; | |
185 | } | |
186 | ||
a924b04d TG |
187 | static inline int common_clock_get(const clockid_t which_clock, |
188 | struct timespec *tp) | |
1da177e4 LT |
189 | { |
190 | getnstimeofday(tp); | |
191 | return 0; | |
192 | } | |
193 | ||
a924b04d TG |
194 | static inline int common_clock_set(const clockid_t which_clock, |
195 | struct timespec *tp) | |
1da177e4 LT |
196 | { |
197 | return do_sys_settimeofday(tp, NULL); | |
198 | } | |
199 | ||
200 | static inline int common_timer_create(struct k_itimer *new_timer) | |
201 | { | |
202 | INIT_LIST_HEAD(&new_timer->it.real.abs_timer_entry); | |
203 | init_timer(&new_timer->it.real.timer); | |
204 | new_timer->it.real.timer.data = (unsigned long) new_timer; | |
205 | new_timer->it.real.timer.function = posix_timer_fn; | |
1da177e4 LT |
206 | return 0; |
207 | } | |
208 | ||
209 | /* | |
210 | * These ones are defined below. | |
211 | */ | |
a924b04d | 212 | static int common_nsleep(const clockid_t, int flags, struct timespec *t); |
1da177e4 LT |
213 | static void common_timer_get(struct k_itimer *, struct itimerspec *); |
214 | static int common_timer_set(struct k_itimer *, int, | |
215 | struct itimerspec *, struct itimerspec *); | |
216 | static int common_timer_del(struct k_itimer *timer); | |
217 | ||
218 | /* | |
219 | * Return nonzero iff we know a priori this clockid_t value is bogus. | |
220 | */ | |
a924b04d | 221 | static inline int invalid_clockid(const clockid_t which_clock) |
1da177e4 LT |
222 | { |
223 | if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */ | |
224 | return 0; | |
225 | if ((unsigned) which_clock >= MAX_CLOCKS) | |
226 | return 1; | |
227 | if (posix_clocks[which_clock].clock_getres != NULL) | |
228 | return 0; | |
229 | #ifndef CLOCK_DISPATCH_DIRECT | |
230 | if (posix_clocks[which_clock].res != 0) | |
231 | return 0; | |
232 | #endif | |
233 | return 1; | |
234 | } | |
235 | ||
236 | ||
237 | /* | |
238 | * Initialize everything, well, just everything in Posix clocks/timers ;) | |
239 | */ | |
240 | static __init int init_posix_timers(void) | |
241 | { | |
242 | struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES, | |
243 | .abs_struct = &abs_list | |
244 | }; | |
245 | struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES, | |
246 | .abs_struct = NULL, | |
247 | .clock_get = do_posix_clock_monotonic_get, | |
248 | .clock_set = do_posix_clock_nosettime | |
249 | }; | |
250 | ||
251 | register_posix_clock(CLOCK_REALTIME, &clock_realtime); | |
252 | register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic); | |
253 | ||
254 | posix_timers_cache = kmem_cache_create("posix_timers_cache", | |
255 | sizeof (struct k_itimer), 0, 0, NULL, NULL); | |
256 | idr_init(&posix_timers_id); | |
257 | return 0; | |
258 | } | |
259 | ||
260 | __initcall(init_posix_timers); | |
261 | ||
262 | static void tstojiffie(struct timespec *tp, int res, u64 *jiff) | |
263 | { | |
264 | long sec = tp->tv_sec; | |
265 | long nsec = tp->tv_nsec + res - 1; | |
266 | ||
3f39894d | 267 | if (nsec >= NSEC_PER_SEC) { |
1da177e4 LT |
268 | sec++; |
269 | nsec -= NSEC_PER_SEC; | |
270 | } | |
271 | ||
272 | /* | |
273 | * The scaling constants are defined in <linux/time.h> | |
274 | * The difference between there and here is that we do the | |
275 | * res rounding and compute a 64-bit result (well so does that | |
276 | * but it then throws away the high bits). | |
277 | */ | |
278 | *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) + | |
279 | (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >> | |
280 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; | |
281 | } | |
282 | ||
283 | /* | |
284 | * This function adjusts the timer as needed as a result of the clock | |
285 | * being set. It should only be called for absolute timers, and then | |
286 | * under the abs_list lock. It computes the time difference and sets | |
287 | * the new jiffies value in the timer. It also updates the timers | |
288 | * reference wall_to_monotonic value. It is complicated by the fact | |
289 | * that tstojiffies() only handles positive times and it needs to work | |
290 | * with both positive and negative times. Also, for negative offsets, | |
291 | * we need to defeat the res round up. | |
292 | * | |
293 | * Return is true if there is a new time, else false. | |
294 | */ | |
295 | static long add_clockset_delta(struct k_itimer *timr, | |
296 | struct timespec *new_wall_to) | |
297 | { | |
298 | struct timespec delta; | |
299 | int sign = 0; | |
300 | u64 exp; | |
301 | ||
302 | set_normalized_timespec(&delta, | |
303 | new_wall_to->tv_sec - | |
304 | timr->it.real.wall_to_prev.tv_sec, | |
305 | new_wall_to->tv_nsec - | |
306 | timr->it.real.wall_to_prev.tv_nsec); | |
307 | if (likely(!(delta.tv_sec | delta.tv_nsec))) | |
308 | return 0; | |
309 | if (delta.tv_sec < 0) { | |
310 | set_normalized_timespec(&delta, | |
311 | -delta.tv_sec, | |
312 | 1 - delta.tv_nsec - | |
313 | posix_clocks[timr->it_clock].res); | |
314 | sign++; | |
315 | } | |
316 | tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp); | |
317 | timr->it.real.wall_to_prev = *new_wall_to; | |
318 | timr->it.real.timer.expires += (sign ? -exp : exp); | |
319 | return 1; | |
320 | } | |
321 | ||
322 | static void remove_from_abslist(struct k_itimer *timr) | |
323 | { | |
324 | if (!list_empty(&timr->it.real.abs_timer_entry)) { | |
325 | spin_lock(&abs_list.lock); | |
326 | list_del_init(&timr->it.real.abs_timer_entry); | |
327 | spin_unlock(&abs_list.lock); | |
328 | } | |
329 | } | |
330 | ||
331 | static void schedule_next_timer(struct k_itimer *timr) | |
332 | { | |
333 | struct timespec new_wall_to; | |
334 | struct now_struct now; | |
335 | unsigned long seq; | |
336 | ||
337 | /* | |
338 | * Set up the timer for the next interval (if there is one). | |
339 | * Note: this code uses the abs_timer_lock to protect | |
340 | * it.real.wall_to_prev and must hold it until exp is set, not exactly | |
341 | * obvious... | |
342 | ||
343 | * This function is used for CLOCK_REALTIME* and | |
344 | * CLOCK_MONOTONIC* timers. If we ever want to handle other | |
345 | * CLOCKs, the calling code (do_schedule_next_timer) would need | |
346 | * to pull the "clock" info from the timer and dispatch the | |
347 | * "other" CLOCKs "next timer" code (which, I suppose should | |
348 | * also be added to the k_clock structure). | |
349 | */ | |
350 | if (!timr->it.real.incr) | |
351 | return; | |
352 | ||
353 | do { | |
354 | seq = read_seqbegin(&xtime_lock); | |
355 | new_wall_to = wall_to_monotonic; | |
356 | posix_get_now(&now); | |
357 | } while (read_seqretry(&xtime_lock, seq)); | |
358 | ||
359 | if (!list_empty(&timr->it.real.abs_timer_entry)) { | |
360 | spin_lock(&abs_list.lock); | |
361 | add_clockset_delta(timr, &new_wall_to); | |
362 | ||
363 | posix_bump_timer(timr, now); | |
364 | ||
365 | spin_unlock(&abs_list.lock); | |
366 | } else { | |
367 | posix_bump_timer(timr, now); | |
368 | } | |
369 | timr->it_overrun_last = timr->it_overrun; | |
370 | timr->it_overrun = -1; | |
371 | ++timr->it_requeue_pending; | |
372 | add_timer(&timr->it.real.timer); | |
373 | } | |
374 | ||
375 | /* | |
376 | * This function is exported for use by the signal deliver code. It is | |
377 | * called just prior to the info block being released and passes that | |
378 | * block to us. It's function is to update the overrun entry AND to | |
379 | * restart the timer. It should only be called if the timer is to be | |
380 | * restarted (i.e. we have flagged this in the sys_private entry of the | |
381 | * info block). | |
382 | * | |
383 | * To protect aginst the timer going away while the interrupt is queued, | |
384 | * we require that the it_requeue_pending flag be set. | |
385 | */ | |
386 | void do_schedule_next_timer(struct siginfo *info) | |
387 | { | |
388 | struct k_itimer *timr; | |
389 | unsigned long flags; | |
390 | ||
391 | timr = lock_timer(info->si_tid, &flags); | |
392 | ||
393 | if (!timr || timr->it_requeue_pending != info->si_sys_private) | |
394 | goto exit; | |
395 | ||
396 | if (timr->it_clock < 0) /* CPU clock */ | |
397 | posix_cpu_timer_schedule(timr); | |
398 | else | |
399 | schedule_next_timer(timr); | |
400 | info->si_overrun = timr->it_overrun_last; | |
401 | exit: | |
402 | if (timr) | |
403 | unlock_timer(timr, flags); | |
404 | } | |
405 | ||
406 | int posix_timer_event(struct k_itimer *timr,int si_private) | |
407 | { | |
408 | memset(&timr->sigq->info, 0, sizeof(siginfo_t)); | |
409 | timr->sigq->info.si_sys_private = si_private; | |
410 | /* | |
411 | * Send signal to the process that owns this timer. | |
412 | ||
413 | * This code assumes that all the possible abs_lists share the | |
414 | * same lock (there is only one list at this time). If this is | |
415 | * not the case, the CLOCK info would need to be used to find | |
416 | * the proper abs list lock. | |
417 | */ | |
418 | ||
419 | timr->sigq->info.si_signo = timr->it_sigev_signo; | |
420 | timr->sigq->info.si_errno = 0; | |
421 | timr->sigq->info.si_code = SI_TIMER; | |
422 | timr->sigq->info.si_tid = timr->it_id; | |
423 | timr->sigq->info.si_value = timr->it_sigev_value; | |
e752dd6c | 424 | |
1da177e4 | 425 | if (timr->it_sigev_notify & SIGEV_THREAD_ID) { |
e752dd6c ON |
426 | struct task_struct *leader; |
427 | int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq, | |
428 | timr->it_process); | |
429 | ||
430 | if (likely(ret >= 0)) | |
431 | return ret; | |
432 | ||
433 | timr->it_sigev_notify = SIGEV_SIGNAL; | |
434 | leader = timr->it_process->group_leader; | |
435 | put_task_struct(timr->it_process); | |
436 | timr->it_process = leader; | |
1da177e4 | 437 | } |
e752dd6c ON |
438 | |
439 | return send_group_sigqueue(timr->it_sigev_signo, timr->sigq, | |
440 | timr->it_process); | |
1da177e4 LT |
441 | } |
442 | EXPORT_SYMBOL_GPL(posix_timer_event); | |
443 | ||
444 | /* | |
445 | * This function gets called when a POSIX.1b interval timer expires. It | |
446 | * is used as a callback from the kernel internal timer. The | |
447 | * run_timer_list code ALWAYS calls with interrupts on. | |
448 | ||
449 | * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. | |
450 | */ | |
451 | static void posix_timer_fn(unsigned long __data) | |
452 | { | |
453 | struct k_itimer *timr = (struct k_itimer *) __data; | |
454 | unsigned long flags; | |
455 | unsigned long seq; | |
456 | struct timespec delta, new_wall_to; | |
457 | u64 exp = 0; | |
458 | int do_notify = 1; | |
459 | ||
460 | spin_lock_irqsave(&timr->it_lock, flags); | |
1da177e4 LT |
461 | if (!list_empty(&timr->it.real.abs_timer_entry)) { |
462 | spin_lock(&abs_list.lock); | |
463 | do { | |
464 | seq = read_seqbegin(&xtime_lock); | |
465 | new_wall_to = wall_to_monotonic; | |
466 | } while (read_seqretry(&xtime_lock, seq)); | |
467 | set_normalized_timespec(&delta, | |
468 | new_wall_to.tv_sec - | |
469 | timr->it.real.wall_to_prev.tv_sec, | |
470 | new_wall_to.tv_nsec - | |
471 | timr->it.real.wall_to_prev.tv_nsec); | |
472 | if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) { | |
473 | /* do nothing, timer is on time */ | |
474 | } else if (delta.tv_sec < 0) { | |
475 | /* do nothing, timer is already late */ | |
476 | } else { | |
477 | /* timer is early due to a clock set */ | |
478 | tstojiffie(&delta, | |
479 | posix_clocks[timr->it_clock].res, | |
480 | &exp); | |
481 | timr->it.real.wall_to_prev = new_wall_to; | |
482 | timr->it.real.timer.expires += exp; | |
483 | add_timer(&timr->it.real.timer); | |
484 | do_notify = 0; | |
485 | } | |
486 | spin_unlock(&abs_list.lock); | |
487 | ||
488 | } | |
489 | if (do_notify) { | |
490 | int si_private=0; | |
491 | ||
492 | if (timr->it.real.incr) | |
493 | si_private = ++timr->it_requeue_pending; | |
494 | else { | |
495 | remove_from_abslist(timr); | |
496 | } | |
497 | ||
498 | if (posix_timer_event(timr, si_private)) | |
499 | /* | |
500 | * signal was not sent because of sig_ignor | |
501 | * we will not get a call back to restart it AND | |
502 | * it should be restarted. | |
503 | */ | |
504 | schedule_next_timer(timr); | |
505 | } | |
506 | unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */ | |
507 | } | |
508 | ||
509 | ||
510 | static inline struct task_struct * good_sigevent(sigevent_t * event) | |
511 | { | |
512 | struct task_struct *rtn = current->group_leader; | |
513 | ||
514 | if ((event->sigev_notify & SIGEV_THREAD_ID ) && | |
515 | (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) || | |
516 | rtn->tgid != current->tgid || | |
517 | (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL)) | |
518 | return NULL; | |
519 | ||
520 | if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) && | |
521 | ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX))) | |
522 | return NULL; | |
523 | ||
524 | return rtn; | |
525 | } | |
526 | ||
a924b04d | 527 | void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock) |
1da177e4 LT |
528 | { |
529 | if ((unsigned) clock_id >= MAX_CLOCKS) { | |
530 | printk("POSIX clock register failed for clock_id %d\n", | |
531 | clock_id); | |
532 | return; | |
533 | } | |
534 | ||
535 | posix_clocks[clock_id] = *new_clock; | |
536 | } | |
537 | EXPORT_SYMBOL_GPL(register_posix_clock); | |
538 | ||
539 | static struct k_itimer * alloc_posix_timer(void) | |
540 | { | |
541 | struct k_itimer *tmr; | |
542 | tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL); | |
543 | if (!tmr) | |
544 | return tmr; | |
545 | memset(tmr, 0, sizeof (struct k_itimer)); | |
546 | if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { | |
547 | kmem_cache_free(posix_timers_cache, tmr); | |
548 | tmr = NULL; | |
549 | } | |
550 | return tmr; | |
551 | } | |
552 | ||
553 | #define IT_ID_SET 1 | |
554 | #define IT_ID_NOT_SET 0 | |
555 | static void release_posix_timer(struct k_itimer *tmr, int it_id_set) | |
556 | { | |
557 | if (it_id_set) { | |
558 | unsigned long flags; | |
559 | spin_lock_irqsave(&idr_lock, flags); | |
560 | idr_remove(&posix_timers_id, tmr->it_id); | |
561 | spin_unlock_irqrestore(&idr_lock, flags); | |
562 | } | |
563 | sigqueue_free(tmr->sigq); | |
564 | if (unlikely(tmr->it_process) && | |
565 | tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) | |
566 | put_task_struct(tmr->it_process); | |
567 | kmem_cache_free(posix_timers_cache, tmr); | |
568 | } | |
569 | ||
570 | /* Create a POSIX.1b interval timer. */ | |
571 | ||
572 | asmlinkage long | |
a924b04d | 573 | sys_timer_create(const clockid_t which_clock, |
1da177e4 LT |
574 | struct sigevent __user *timer_event_spec, |
575 | timer_t __user * created_timer_id) | |
576 | { | |
577 | int error = 0; | |
578 | struct k_itimer *new_timer = NULL; | |
579 | int new_timer_id; | |
580 | struct task_struct *process = NULL; | |
581 | unsigned long flags; | |
582 | sigevent_t event; | |
583 | int it_id_set = IT_ID_NOT_SET; | |
584 | ||
585 | if (invalid_clockid(which_clock)) | |
586 | return -EINVAL; | |
587 | ||
588 | new_timer = alloc_posix_timer(); | |
589 | if (unlikely(!new_timer)) | |
590 | return -EAGAIN; | |
591 | ||
592 | spin_lock_init(&new_timer->it_lock); | |
593 | retry: | |
594 | if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) { | |
595 | error = -EAGAIN; | |
596 | goto out; | |
597 | } | |
598 | spin_lock_irq(&idr_lock); | |
599 | error = idr_get_new(&posix_timers_id, | |
600 | (void *) new_timer, | |
601 | &new_timer_id); | |
602 | spin_unlock_irq(&idr_lock); | |
603 | if (error == -EAGAIN) | |
604 | goto retry; | |
605 | else if (error) { | |
606 | /* | |
607 | * Wierd looking, but we return EAGAIN if the IDR is | |
608 | * full (proper POSIX return value for this) | |
609 | */ | |
610 | error = -EAGAIN; | |
611 | goto out; | |
612 | } | |
613 | ||
614 | it_id_set = IT_ID_SET; | |
615 | new_timer->it_id = (timer_t) new_timer_id; | |
616 | new_timer->it_clock = which_clock; | |
617 | new_timer->it_overrun = -1; | |
618 | error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer)); | |
619 | if (error) | |
620 | goto out; | |
621 | ||
622 | /* | |
623 | * return the timer_id now. The next step is hard to | |
624 | * back out if there is an error. | |
625 | */ | |
626 | if (copy_to_user(created_timer_id, | |
627 | &new_timer_id, sizeof (new_timer_id))) { | |
628 | error = -EFAULT; | |
629 | goto out; | |
630 | } | |
631 | if (timer_event_spec) { | |
632 | if (copy_from_user(&event, timer_event_spec, sizeof (event))) { | |
633 | error = -EFAULT; | |
634 | goto out; | |
635 | } | |
636 | new_timer->it_sigev_notify = event.sigev_notify; | |
637 | new_timer->it_sigev_signo = event.sigev_signo; | |
638 | new_timer->it_sigev_value = event.sigev_value; | |
639 | ||
640 | read_lock(&tasklist_lock); | |
641 | if ((process = good_sigevent(&event))) { | |
642 | /* | |
643 | * We may be setting up this process for another | |
644 | * thread. It may be exiting. To catch this | |
645 | * case the we check the PF_EXITING flag. If | |
646 | * the flag is not set, the siglock will catch | |
647 | * him before it is too late (in exit_itimers). | |
648 | * | |
649 | * The exec case is a bit more invloved but easy | |
650 | * to code. If the process is in our thread | |
651 | * group (and it must be or we would not allow | |
652 | * it here) and is doing an exec, it will cause | |
653 | * us to be killed. In this case it will wait | |
654 | * for us to die which means we can finish this | |
655 | * linkage with our last gasp. I.e. no code :) | |
656 | */ | |
657 | spin_lock_irqsave(&process->sighand->siglock, flags); | |
658 | if (!(process->flags & PF_EXITING)) { | |
659 | new_timer->it_process = process; | |
660 | list_add(&new_timer->list, | |
661 | &process->signal->posix_timers); | |
662 | spin_unlock_irqrestore(&process->sighand->siglock, flags); | |
663 | if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) | |
664 | get_task_struct(process); | |
665 | } else { | |
666 | spin_unlock_irqrestore(&process->sighand->siglock, flags); | |
667 | process = NULL; | |
668 | } | |
669 | } | |
670 | read_unlock(&tasklist_lock); | |
671 | if (!process) { | |
672 | error = -EINVAL; | |
673 | goto out; | |
674 | } | |
675 | } else { | |
676 | new_timer->it_sigev_notify = SIGEV_SIGNAL; | |
677 | new_timer->it_sigev_signo = SIGALRM; | |
678 | new_timer->it_sigev_value.sival_int = new_timer->it_id; | |
679 | process = current->group_leader; | |
680 | spin_lock_irqsave(&process->sighand->siglock, flags); | |
681 | new_timer->it_process = process; | |
682 | list_add(&new_timer->list, &process->signal->posix_timers); | |
683 | spin_unlock_irqrestore(&process->sighand->siglock, flags); | |
684 | } | |
685 | ||
686 | /* | |
687 | * In the case of the timer belonging to another task, after | |
688 | * the task is unlocked, the timer is owned by the other task | |
689 | * and may cease to exist at any time. Don't use or modify | |
690 | * new_timer after the unlock call. | |
691 | */ | |
692 | ||
693 | out: | |
694 | if (error) | |
695 | release_posix_timer(new_timer, it_id_set); | |
696 | ||
697 | return error; | |
698 | } | |
699 | ||
700 | /* | |
701 | * good_timespec | |
702 | * | |
703 | * This function checks the elements of a timespec structure. | |
704 | * | |
705 | * Arguments: | |
706 | * ts : Pointer to the timespec structure to check | |
707 | * | |
708 | * Return value: | |
709 | * If a NULL pointer was passed in, or the tv_nsec field was less than 0 | |
710 | * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0, | |
711 | * this function returns 0. Otherwise it returns 1. | |
712 | */ | |
713 | static int good_timespec(const struct timespec *ts) | |
714 | { | |
715 | if ((!ts) || (ts->tv_sec < 0) || | |
716 | ((unsigned) ts->tv_nsec >= NSEC_PER_SEC)) | |
717 | return 0; | |
718 | return 1; | |
719 | } | |
720 | ||
721 | /* | |
722 | * Locking issues: We need to protect the result of the id look up until | |
723 | * we get the timer locked down so it is not deleted under us. The | |
724 | * removal is done under the idr spinlock so we use that here to bridge | |
725 | * the find to the timer lock. To avoid a dead lock, the timer id MUST | |
726 | * be release with out holding the timer lock. | |
727 | */ | |
728 | static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags) | |
729 | { | |
730 | struct k_itimer *timr; | |
731 | /* | |
732 | * Watch out here. We do a irqsave on the idr_lock and pass the | |
733 | * flags part over to the timer lock. Must not let interrupts in | |
734 | * while we are moving the lock. | |
735 | */ | |
736 | ||
737 | spin_lock_irqsave(&idr_lock, *flags); | |
738 | timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id); | |
739 | if (timr) { | |
740 | spin_lock(&timr->it_lock); | |
741 | spin_unlock(&idr_lock); | |
742 | ||
743 | if ((timr->it_id != timer_id) || !(timr->it_process) || | |
744 | timr->it_process->tgid != current->tgid) { | |
745 | unlock_timer(timr, *flags); | |
746 | timr = NULL; | |
747 | } | |
748 | } else | |
749 | spin_unlock_irqrestore(&idr_lock, *flags); | |
750 | ||
751 | return timr; | |
752 | } | |
753 | ||
754 | /* | |
755 | * Get the time remaining on a POSIX.1b interval timer. This function | |
756 | * is ALWAYS called with spin_lock_irq on the timer, thus it must not | |
757 | * mess with irq. | |
758 | * | |
759 | * We have a couple of messes to clean up here. First there is the case | |
760 | * of a timer that has a requeue pending. These timers should appear to | |
761 | * be in the timer list with an expiry as if we were to requeue them | |
762 | * now. | |
763 | * | |
764 | * The second issue is the SIGEV_NONE timer which may be active but is | |
765 | * not really ever put in the timer list (to save system resources). | |
766 | * This timer may be expired, and if so, we will do it here. Otherwise | |
767 | * it is the same as a requeue pending timer WRT to what we should | |
768 | * report. | |
769 | */ | |
770 | static void | |
771 | common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) | |
772 | { | |
773 | unsigned long expires; | |
774 | struct now_struct now; | |
775 | ||
776 | do | |
777 | expires = timr->it.real.timer.expires; | |
778 | while ((volatile long) (timr->it.real.timer.expires) != expires); | |
779 | ||
780 | posix_get_now(&now); | |
781 | ||
782 | if (expires && | |
783 | ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) && | |
784 | !timr->it.real.incr && | |
785 | posix_time_before(&timr->it.real.timer, &now)) | |
786 | timr->it.real.timer.expires = expires = 0; | |
787 | if (expires) { | |
788 | if (timr->it_requeue_pending & REQUEUE_PENDING || | |
789 | (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { | |
790 | posix_bump_timer(timr, now); | |
791 | expires = timr->it.real.timer.expires; | |
792 | } | |
793 | else | |
794 | if (!timer_pending(&timr->it.real.timer)) | |
795 | expires = 0; | |
796 | if (expires) | |
797 | expires -= now.jiffies; | |
798 | } | |
799 | jiffies_to_timespec(expires, &cur_setting->it_value); | |
800 | jiffies_to_timespec(timr->it.real.incr, &cur_setting->it_interval); | |
801 | ||
802 | if (cur_setting->it_value.tv_sec < 0) { | |
803 | cur_setting->it_value.tv_nsec = 1; | |
804 | cur_setting->it_value.tv_sec = 0; | |
805 | } | |
806 | } | |
807 | ||
808 | /* Get the time remaining on a POSIX.1b interval timer. */ | |
809 | asmlinkage long | |
810 | sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting) | |
811 | { | |
812 | struct k_itimer *timr; | |
813 | struct itimerspec cur_setting; | |
814 | unsigned long flags; | |
815 | ||
816 | timr = lock_timer(timer_id, &flags); | |
817 | if (!timr) | |
818 | return -EINVAL; | |
819 | ||
820 | CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting)); | |
821 | ||
822 | unlock_timer(timr, flags); | |
823 | ||
824 | if (copy_to_user(setting, &cur_setting, sizeof (cur_setting))) | |
825 | return -EFAULT; | |
826 | ||
827 | return 0; | |
828 | } | |
829 | /* | |
830 | * Get the number of overruns of a POSIX.1b interval timer. This is to | |
831 | * be the overrun of the timer last delivered. At the same time we are | |
832 | * accumulating overruns on the next timer. The overrun is frozen when | |
833 | * the signal is delivered, either at the notify time (if the info block | |
834 | * is not queued) or at the actual delivery time (as we are informed by | |
835 | * the call back to do_schedule_next_timer(). So all we need to do is | |
836 | * to pick up the frozen overrun. | |
837 | */ | |
838 | ||
839 | asmlinkage long | |
840 | sys_timer_getoverrun(timer_t timer_id) | |
841 | { | |
842 | struct k_itimer *timr; | |
843 | int overrun; | |
844 | long flags; | |
845 | ||
846 | timr = lock_timer(timer_id, &flags); | |
847 | if (!timr) | |
848 | return -EINVAL; | |
849 | ||
850 | overrun = timr->it_overrun_last; | |
851 | unlock_timer(timr, flags); | |
852 | ||
853 | return overrun; | |
854 | } | |
855 | /* | |
856 | * Adjust for absolute time | |
857 | * | |
858 | * If absolute time is given and it is not CLOCK_MONOTONIC, we need to | |
859 | * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and | |
860 | * what ever clock he is using. | |
861 | * | |
862 | * If it is relative time, we need to add the current (CLOCK_MONOTONIC) | |
863 | * time to it to get the proper time for the timer. | |
864 | */ | |
865 | static int adjust_abs_time(struct k_clock *clock, struct timespec *tp, | |
866 | int abs, u64 *exp, struct timespec *wall_to) | |
867 | { | |
868 | struct timespec now; | |
869 | struct timespec oc = *tp; | |
870 | u64 jiffies_64_f; | |
871 | int rtn =0; | |
872 | ||
873 | if (abs) { | |
874 | /* | |
875 | * The mask pick up the 4 basic clocks | |
876 | */ | |
877 | if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) { | |
878 | jiffies_64_f = do_posix_clock_monotonic_gettime_parts( | |
879 | &now, wall_to); | |
880 | /* | |
881 | * If we are doing a MONOTONIC clock | |
882 | */ | |
883 | if((clock - &posix_clocks[0]) & CLOCKS_MONO){ | |
884 | now.tv_sec += wall_to->tv_sec; | |
885 | now.tv_nsec += wall_to->tv_nsec; | |
886 | } | |
887 | } else { | |
888 | /* | |
889 | * Not one of the basic clocks | |
890 | */ | |
891 | clock->clock_get(clock - posix_clocks, &now); | |
892 | jiffies_64_f = get_jiffies_64(); | |
893 | } | |
894 | /* | |
78fa74a2 | 895 | * Take away now to get delta and normalize |
1da177e4 | 896 | */ |
78fa74a2 GA |
897 | set_normalized_timespec(&oc, oc.tv_sec - now.tv_sec, |
898 | oc.tv_nsec - now.tv_nsec); | |
1da177e4 LT |
899 | }else{ |
900 | jiffies_64_f = get_jiffies_64(); | |
901 | } | |
902 | /* | |
903 | * Check if the requested time is prior to now (if so set now) | |
904 | */ | |
905 | if (oc.tv_sec < 0) | |
906 | oc.tv_sec = oc.tv_nsec = 0; | |
907 | ||
908 | if (oc.tv_sec | oc.tv_nsec) | |
909 | set_normalized_timespec(&oc, oc.tv_sec, | |
910 | oc.tv_nsec + clock->res); | |
911 | tstojiffie(&oc, clock->res, exp); | |
912 | ||
913 | /* | |
914 | * Check if the requested time is more than the timer code | |
915 | * can handle (if so we error out but return the value too). | |
916 | */ | |
917 | if (*exp > ((u64)MAX_JIFFY_OFFSET)) | |
918 | /* | |
919 | * This is a considered response, not exactly in | |
920 | * line with the standard (in fact it is silent on | |
921 | * possible overflows). We assume such a large | |
922 | * value is ALMOST always a programming error and | |
923 | * try not to compound it by setting a really dumb | |
924 | * value. | |
925 | */ | |
926 | rtn = -EINVAL; | |
927 | /* | |
928 | * return the actual jiffies expire time, full 64 bits | |
929 | */ | |
930 | *exp += jiffies_64_f; | |
931 | return rtn; | |
932 | } | |
933 | ||
934 | /* Set a POSIX.1b interval timer. */ | |
935 | /* timr->it_lock is taken. */ | |
936 | static inline int | |
937 | common_timer_set(struct k_itimer *timr, int flags, | |
938 | struct itimerspec *new_setting, struct itimerspec *old_setting) | |
939 | { | |
940 | struct k_clock *clock = &posix_clocks[timr->it_clock]; | |
941 | u64 expire_64; | |
942 | ||
943 | if (old_setting) | |
944 | common_timer_get(timr, old_setting); | |
945 | ||
946 | /* disable the timer */ | |
947 | timr->it.real.incr = 0; | |
948 | /* | |
949 | * careful here. If smp we could be in the "fire" routine which will | |
950 | * be spinning as we hold the lock. But this is ONLY an SMP issue. | |
951 | */ | |
f972be33 | 952 | if (try_to_del_timer_sync(&timr->it.real.timer) < 0) { |
1da177e4 | 953 | #ifdef CONFIG_SMP |
1da177e4 LT |
954 | /* |
955 | * It can only be active if on an other cpu. Since | |
956 | * we have cleared the interval stuff above, it should | |
957 | * clear once we release the spin lock. Of course once | |
958 | * we do that anything could happen, including the | |
959 | * complete melt down of the timer. So return with | |
960 | * a "retry" exit status. | |
961 | */ | |
962 | return TIMER_RETRY; | |
1da177e4 | 963 | #endif |
f972be33 ON |
964 | } |
965 | ||
1da177e4 LT |
966 | remove_from_abslist(timr); |
967 | ||
968 | timr->it_requeue_pending = (timr->it_requeue_pending + 2) & | |
969 | ~REQUEUE_PENDING; | |
970 | timr->it_overrun_last = 0; | |
971 | timr->it_overrun = -1; | |
972 | /* | |
973 | *switch off the timer when it_value is zero | |
974 | */ | |
975 | if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) { | |
976 | timr->it.real.timer.expires = 0; | |
977 | return 0; | |
978 | } | |
979 | ||
980 | if (adjust_abs_time(clock, | |
981 | &new_setting->it_value, flags & TIMER_ABSTIME, | |
982 | &expire_64, &(timr->it.real.wall_to_prev))) { | |
983 | return -EINVAL; | |
984 | } | |
985 | timr->it.real.timer.expires = (unsigned long)expire_64; | |
986 | tstojiffie(&new_setting->it_interval, clock->res, &expire_64); | |
987 | timr->it.real.incr = (unsigned long)expire_64; | |
988 | ||
989 | /* | |
990 | * We do not even queue SIGEV_NONE timers! But we do put them | |
991 | * in the abs list so we can do that right. | |
992 | */ | |
993 | if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)) | |
994 | add_timer(&timr->it.real.timer); | |
995 | ||
996 | if (flags & TIMER_ABSTIME && clock->abs_struct) { | |
997 | spin_lock(&clock->abs_struct->lock); | |
998 | list_add_tail(&(timr->it.real.abs_timer_entry), | |
999 | &(clock->abs_struct->list)); | |
1000 | spin_unlock(&clock->abs_struct->lock); | |
1001 | } | |
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | /* Set a POSIX.1b interval timer */ | |
1006 | asmlinkage long | |
1007 | sys_timer_settime(timer_t timer_id, int flags, | |
1008 | const struct itimerspec __user *new_setting, | |
1009 | struct itimerspec __user *old_setting) | |
1010 | { | |
1011 | struct k_itimer *timr; | |
1012 | struct itimerspec new_spec, old_spec; | |
1013 | int error = 0; | |
1014 | long flag; | |
1015 | struct itimerspec *rtn = old_setting ? &old_spec : NULL; | |
1016 | ||
1017 | if (!new_setting) | |
1018 | return -EINVAL; | |
1019 | ||
1020 | if (copy_from_user(&new_spec, new_setting, sizeof (new_spec))) | |
1021 | return -EFAULT; | |
1022 | ||
1023 | if ((!good_timespec(&new_spec.it_interval)) || | |
1024 | (!good_timespec(&new_spec.it_value))) | |
1025 | return -EINVAL; | |
1026 | retry: | |
1027 | timr = lock_timer(timer_id, &flag); | |
1028 | if (!timr) | |
1029 | return -EINVAL; | |
1030 | ||
1031 | error = CLOCK_DISPATCH(timr->it_clock, timer_set, | |
1032 | (timr, flags, &new_spec, rtn)); | |
1033 | ||
1034 | unlock_timer(timr, flag); | |
1035 | if (error == TIMER_RETRY) { | |
1036 | rtn = NULL; // We already got the old time... | |
1037 | goto retry; | |
1038 | } | |
1039 | ||
1040 | if (old_setting && !error && copy_to_user(old_setting, | |
1041 | &old_spec, sizeof (old_spec))) | |
1042 | error = -EFAULT; | |
1043 | ||
1044 | return error; | |
1045 | } | |
1046 | ||
1047 | static inline int common_timer_del(struct k_itimer *timer) | |
1048 | { | |
1049 | timer->it.real.incr = 0; | |
f972be33 ON |
1050 | |
1051 | if (try_to_del_timer_sync(&timer->it.real.timer) < 0) { | |
1da177e4 | 1052 | #ifdef CONFIG_SMP |
1da177e4 LT |
1053 | /* |
1054 | * It can only be active if on an other cpu. Since | |
1055 | * we have cleared the interval stuff above, it should | |
1056 | * clear once we release the spin lock. Of course once | |
1057 | * we do that anything could happen, including the | |
1058 | * complete melt down of the timer. So return with | |
1059 | * a "retry" exit status. | |
1060 | */ | |
1061 | return TIMER_RETRY; | |
1da177e4 | 1062 | #endif |
f972be33 ON |
1063 | } |
1064 | ||
1da177e4 LT |
1065 | remove_from_abslist(timer); |
1066 | ||
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | static inline int timer_delete_hook(struct k_itimer *timer) | |
1071 | { | |
1072 | return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer)); | |
1073 | } | |
1074 | ||
1075 | /* Delete a POSIX.1b interval timer. */ | |
1076 | asmlinkage long | |
1077 | sys_timer_delete(timer_t timer_id) | |
1078 | { | |
1079 | struct k_itimer *timer; | |
1080 | long flags; | |
1081 | ||
1082 | #ifdef CONFIG_SMP | |
1083 | int error; | |
1084 | retry_delete: | |
1085 | #endif | |
1086 | timer = lock_timer(timer_id, &flags); | |
1087 | if (!timer) | |
1088 | return -EINVAL; | |
1089 | ||
1090 | #ifdef CONFIG_SMP | |
1091 | error = timer_delete_hook(timer); | |
1092 | ||
1093 | if (error == TIMER_RETRY) { | |
1094 | unlock_timer(timer, flags); | |
1095 | goto retry_delete; | |
1096 | } | |
1097 | #else | |
1098 | timer_delete_hook(timer); | |
1099 | #endif | |
1100 | spin_lock(¤t->sighand->siglock); | |
1101 | list_del(&timer->list); | |
1102 | spin_unlock(¤t->sighand->siglock); | |
1103 | /* | |
1104 | * This keeps any tasks waiting on the spin lock from thinking | |
1105 | * they got something (see the lock code above). | |
1106 | */ | |
1107 | if (timer->it_process) { | |
1108 | if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) | |
1109 | put_task_struct(timer->it_process); | |
1110 | timer->it_process = NULL; | |
1111 | } | |
1112 | unlock_timer(timer, flags); | |
1113 | release_posix_timer(timer, IT_ID_SET); | |
1114 | return 0; | |
1115 | } | |
1116 | /* | |
1117 | * return timer owned by the process, used by exit_itimers | |
1118 | */ | |
1119 | static inline void itimer_delete(struct k_itimer *timer) | |
1120 | { | |
1121 | unsigned long flags; | |
1122 | ||
1123 | #ifdef CONFIG_SMP | |
1124 | int error; | |
1125 | retry_delete: | |
1126 | #endif | |
1127 | spin_lock_irqsave(&timer->it_lock, flags); | |
1128 | ||
1129 | #ifdef CONFIG_SMP | |
1130 | error = timer_delete_hook(timer); | |
1131 | ||
1132 | if (error == TIMER_RETRY) { | |
1133 | unlock_timer(timer, flags); | |
1134 | goto retry_delete; | |
1135 | } | |
1136 | #else | |
1137 | timer_delete_hook(timer); | |
1138 | #endif | |
1139 | list_del(&timer->list); | |
1140 | /* | |
1141 | * This keeps any tasks waiting on the spin lock from thinking | |
1142 | * they got something (see the lock code above). | |
1143 | */ | |
1144 | if (timer->it_process) { | |
1145 | if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) | |
1146 | put_task_struct(timer->it_process); | |
1147 | timer->it_process = NULL; | |
1148 | } | |
1149 | unlock_timer(timer, flags); | |
1150 | release_posix_timer(timer, IT_ID_SET); | |
1151 | } | |
1152 | ||
1153 | /* | |
25f407f0 | 1154 | * This is called by do_exit or de_thread, only when there are no more |
1da177e4 LT |
1155 | * references to the shared signal_struct. |
1156 | */ | |
1157 | void exit_itimers(struct signal_struct *sig) | |
1158 | { | |
1159 | struct k_itimer *tmr; | |
1160 | ||
1161 | while (!list_empty(&sig->posix_timers)) { | |
1162 | tmr = list_entry(sig->posix_timers.next, struct k_itimer, list); | |
1163 | itimer_delete(tmr); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * And now for the "clock" calls | |
1169 | * | |
1170 | * These functions are called both from timer functions (with the timer | |
1171 | * spin_lock_irq() held and from clock calls with no locking. They must | |
1172 | * use the save flags versions of locks. | |
1173 | */ | |
1174 | ||
1175 | /* | |
1176 | * We do ticks here to avoid the irq lock ( they take sooo long). | |
1177 | * The seqlock is great here. Since we a reader, we don't really care | |
1178 | * if we are interrupted since we don't take lock that will stall us or | |
1179 | * any other cpu. Voila, no irq lock is needed. | |
1180 | * | |
1181 | */ | |
1182 | ||
1183 | static u64 do_posix_clock_monotonic_gettime_parts( | |
1184 | struct timespec *tp, struct timespec *mo) | |
1185 | { | |
1186 | u64 jiff; | |
1187 | unsigned int seq; | |
1188 | ||
1189 | do { | |
1190 | seq = read_seqbegin(&xtime_lock); | |
1191 | getnstimeofday(tp); | |
1192 | *mo = wall_to_monotonic; | |
1193 | jiff = jiffies_64; | |
1194 | ||
1195 | } while(read_seqretry(&xtime_lock, seq)); | |
1196 | ||
1197 | return jiff; | |
1198 | } | |
1199 | ||
a924b04d TG |
1200 | static int do_posix_clock_monotonic_get(const clockid_t clock, |
1201 | struct timespec *tp) | |
1da177e4 LT |
1202 | { |
1203 | struct timespec wall_to_mono; | |
1204 | ||
1205 | do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono); | |
1206 | ||
3f39894d GA |
1207 | set_normalized_timespec(tp, tp->tv_sec + wall_to_mono.tv_sec, |
1208 | tp->tv_nsec + wall_to_mono.tv_nsec); | |
1da177e4 | 1209 | |
1da177e4 LT |
1210 | return 0; |
1211 | } | |
1212 | ||
1213 | int do_posix_clock_monotonic_gettime(struct timespec *tp) | |
1214 | { | |
1215 | return do_posix_clock_monotonic_get(CLOCK_MONOTONIC, tp); | |
1216 | } | |
1217 | ||
a924b04d | 1218 | int do_posix_clock_nosettime(const clockid_t clockid, struct timespec *tp) |
1da177e4 LT |
1219 | { |
1220 | return -EINVAL; | |
1221 | } | |
1222 | EXPORT_SYMBOL_GPL(do_posix_clock_nosettime); | |
1223 | ||
1224 | int do_posix_clock_notimer_create(struct k_itimer *timer) | |
1225 | { | |
1226 | return -EINVAL; | |
1227 | } | |
1228 | EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create); | |
1229 | ||
a924b04d TG |
1230 | int do_posix_clock_nonanosleep(const clockid_t clock, int flags, |
1231 | struct timespec *t) | |
1da177e4 LT |
1232 | { |
1233 | #ifndef ENOTSUP | |
1234 | return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */ | |
1235 | #else /* parisc does define it separately. */ | |
1236 | return -ENOTSUP; | |
1237 | #endif | |
1238 | } | |
1239 | EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep); | |
1240 | ||
a924b04d TG |
1241 | asmlinkage long sys_clock_settime(const clockid_t which_clock, |
1242 | const struct timespec __user *tp) | |
1da177e4 LT |
1243 | { |
1244 | struct timespec new_tp; | |
1245 | ||
1246 | if (invalid_clockid(which_clock)) | |
1247 | return -EINVAL; | |
1248 | if (copy_from_user(&new_tp, tp, sizeof (*tp))) | |
1249 | return -EFAULT; | |
1250 | ||
1251 | return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp)); | |
1252 | } | |
1253 | ||
1254 | asmlinkage long | |
a924b04d | 1255 | sys_clock_gettime(const clockid_t which_clock, struct timespec __user *tp) |
1da177e4 LT |
1256 | { |
1257 | struct timespec kernel_tp; | |
1258 | int error; | |
1259 | ||
1260 | if (invalid_clockid(which_clock)) | |
1261 | return -EINVAL; | |
1262 | error = CLOCK_DISPATCH(which_clock, clock_get, | |
1263 | (which_clock, &kernel_tp)); | |
1264 | if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp))) | |
1265 | error = -EFAULT; | |
1266 | ||
1267 | return error; | |
1268 | ||
1269 | } | |
1270 | ||
1271 | asmlinkage long | |
a924b04d | 1272 | sys_clock_getres(const clockid_t which_clock, struct timespec __user *tp) |
1da177e4 LT |
1273 | { |
1274 | struct timespec rtn_tp; | |
1275 | int error; | |
1276 | ||
1277 | if (invalid_clockid(which_clock)) | |
1278 | return -EINVAL; | |
1279 | ||
1280 | error = CLOCK_DISPATCH(which_clock, clock_getres, | |
1281 | (which_clock, &rtn_tp)); | |
1282 | ||
1283 | if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) { | |
1284 | error = -EFAULT; | |
1285 | } | |
1286 | ||
1287 | return error; | |
1288 | } | |
1289 | ||
1da177e4 LT |
1290 | /* |
1291 | * The standard says that an absolute nanosleep call MUST wake up at | |
1292 | * the requested time in spite of clock settings. Here is what we do: | |
1293 | * For each nanosleep call that needs it (only absolute and not on | |
1294 | * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure | |
1295 | * into the "nanosleep_abs_list". All we need is the task_struct pointer. | |
1296 | * When ever the clock is set we just wake up all those tasks. The rest | |
1297 | * is done by the while loop in clock_nanosleep(). | |
1298 | * | |
1299 | * On locking, clock_was_set() is called from update_wall_clock which | |
1300 | * holds (or has held for it) a write_lock_irq( xtime_lock) and is | |
1301 | * called from the timer bh code. Thus we need the irq save locks. | |
1302 | * | |
1303 | * Also, on the call from update_wall_clock, that is done as part of a | |
1304 | * softirq thing. We don't want to delay the system that much (possibly | |
1305 | * long list of timers to fix), so we defer that work to keventd. | |
1306 | */ | |
1307 | ||
1308 | static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue); | |
1309 | static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL); | |
1310 | ||
1311 | static DECLARE_MUTEX(clock_was_set_lock); | |
1312 | ||
1313 | void clock_was_set(void) | |
1314 | { | |
1315 | struct k_itimer *timr; | |
1316 | struct timespec new_wall_to; | |
1317 | LIST_HEAD(cws_list); | |
1318 | unsigned long seq; | |
1319 | ||
1320 | ||
1321 | if (unlikely(in_interrupt())) { | |
1322 | schedule_work(&clock_was_set_work); | |
1323 | return; | |
1324 | } | |
1325 | wake_up_all(&nanosleep_abs_wqueue); | |
1326 | ||
1327 | /* | |
1328 | * Check if there exist TIMER_ABSTIME timers to correct. | |
1329 | * | |
1330 | * Notes on locking: This code is run in task context with irq | |
1331 | * on. We CAN be interrupted! All other usage of the abs list | |
1332 | * lock is under the timer lock which holds the irq lock as | |
1333 | * well. We REALLY don't want to scan the whole list with the | |
1334 | * interrupt system off, AND we would like a sequence lock on | |
1335 | * this code as well. Since we assume that the clock will not | |
1336 | * be set often, it seems ok to take and release the irq lock | |
1337 | * for each timer. In fact add_timer will do this, so this is | |
1338 | * not an issue. So we know when we are done, we will move the | |
1339 | * whole list to a new location. Then as we process each entry, | |
1340 | * we will move it to the actual list again. This way, when our | |
1341 | * copy is empty, we are done. We are not all that concerned | |
1342 | * about preemption so we will use a semaphore lock to protect | |
1343 | * aginst reentry. This way we will not stall another | |
1344 | * processor. It is possible that this may delay some timers | |
1345 | * that should have expired, given the new clock, but even this | |
1346 | * will be minimal as we will always update to the current time, | |
1347 | * even if it was set by a task that is waiting for entry to | |
1348 | * this code. Timers that expire too early will be caught by | |
1349 | * the expire code and restarted. | |
1350 | ||
1351 | * Absolute timers that repeat are left in the abs list while | |
1352 | * waiting for the task to pick up the signal. This means we | |
1353 | * may find timers that are not in the "add_timer" list, but are | |
1354 | * in the abs list. We do the same thing for these, save | |
1355 | * putting them back in the "add_timer" list. (Note, these are | |
1356 | * left in the abs list mainly to indicate that they are | |
1357 | * ABSOLUTE timers, a fact that is used by the re-arm code, and | |
1358 | * for which we have no other flag.) | |
1359 | ||
1360 | */ | |
1361 | ||
1362 | down(&clock_was_set_lock); | |
1363 | spin_lock_irq(&abs_list.lock); | |
1364 | list_splice_init(&abs_list.list, &cws_list); | |
1365 | spin_unlock_irq(&abs_list.lock); | |
1366 | do { | |
1367 | do { | |
1368 | seq = read_seqbegin(&xtime_lock); | |
1369 | new_wall_to = wall_to_monotonic; | |
1370 | } while (read_seqretry(&xtime_lock, seq)); | |
1371 | ||
1372 | spin_lock_irq(&abs_list.lock); | |
1373 | if (list_empty(&cws_list)) { | |
1374 | spin_unlock_irq(&abs_list.lock); | |
1375 | break; | |
1376 | } | |
1377 | timr = list_entry(cws_list.next, struct k_itimer, | |
1378 | it.real.abs_timer_entry); | |
1379 | ||
1380 | list_del_init(&timr->it.real.abs_timer_entry); | |
1381 | if (add_clockset_delta(timr, &new_wall_to) && | |
1382 | del_timer(&timr->it.real.timer)) /* timer run yet? */ | |
1383 | add_timer(&timr->it.real.timer); | |
1384 | list_add(&timr->it.real.abs_timer_entry, &abs_list.list); | |
1385 | spin_unlock_irq(&abs_list.lock); | |
1386 | } while (1); | |
1387 | ||
1388 | up(&clock_was_set_lock); | |
1389 | } | |
1390 | ||
1391 | long clock_nanosleep_restart(struct restart_block *restart_block); | |
1392 | ||
1393 | asmlinkage long | |
a924b04d | 1394 | sys_clock_nanosleep(const clockid_t which_clock, int flags, |
1da177e4 LT |
1395 | const struct timespec __user *rqtp, |
1396 | struct timespec __user *rmtp) | |
1397 | { | |
1398 | struct timespec t; | |
1399 | struct restart_block *restart_block = | |
1400 | &(current_thread_info()->restart_block); | |
1401 | int ret; | |
1402 | ||
1403 | if (invalid_clockid(which_clock)) | |
1404 | return -EINVAL; | |
1405 | ||
1406 | if (copy_from_user(&t, rqtp, sizeof (struct timespec))) | |
1407 | return -EFAULT; | |
1408 | ||
1409 | if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0) | |
1410 | return -EINVAL; | |
1411 | ||
1412 | /* | |
1413 | * Do this here as nsleep function does not have the real address. | |
1414 | */ | |
1415 | restart_block->arg1 = (unsigned long)rmtp; | |
1416 | ||
1417 | ret = CLOCK_DISPATCH(which_clock, nsleep, (which_clock, flags, &t)); | |
1418 | ||
1419 | if ((ret == -ERESTART_RESTARTBLOCK) && rmtp && | |
1420 | copy_to_user(rmtp, &t, sizeof (t))) | |
1421 | return -EFAULT; | |
1422 | return ret; | |
1423 | } | |
1424 | ||
1425 | ||
a924b04d | 1426 | static int common_nsleep(const clockid_t which_clock, |
1da177e4 LT |
1427 | int flags, struct timespec *tsave) |
1428 | { | |
1429 | struct timespec t, dum; | |
1da177e4 LT |
1430 | DECLARE_WAITQUEUE(abs_wqueue, current); |
1431 | u64 rq_time = (u64)0; | |
1432 | s64 left; | |
1433 | int abs; | |
1434 | struct restart_block *restart_block = | |
1435 | ¤t_thread_info()->restart_block; | |
1436 | ||
1437 | abs_wqueue.flags = 0; | |
1da177e4 LT |
1438 | abs = flags & TIMER_ABSTIME; |
1439 | ||
1440 | if (restart_block->fn == clock_nanosleep_restart) { | |
1441 | /* | |
1442 | * Interrupted by a non-delivered signal, pick up remaining | |
1443 | * time and continue. Remaining time is in arg2 & 3. | |
1444 | */ | |
1445 | restart_block->fn = do_no_restart_syscall; | |
1446 | ||
1447 | rq_time = restart_block->arg3; | |
1448 | rq_time = (rq_time << 32) + restart_block->arg2; | |
1449 | if (!rq_time) | |
1450 | return -EINTR; | |
1451 | left = rq_time - get_jiffies_64(); | |
1452 | if (left <= (s64)0) | |
1453 | return 0; /* Already passed */ | |
1454 | } | |
1455 | ||
1456 | if (abs && (posix_clocks[which_clock].clock_get != | |
1457 | posix_clocks[CLOCK_MONOTONIC].clock_get)) | |
1458 | add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue); | |
1459 | ||
1460 | do { | |
1461 | t = *tsave; | |
1462 | if (abs || !rq_time) { | |
1463 | adjust_abs_time(&posix_clocks[which_clock], &t, abs, | |
1464 | &rq_time, &dum); | |
1465 | } | |
1466 | ||
1467 | left = rq_time - get_jiffies_64(); | |
1468 | if (left >= (s64)MAX_JIFFY_OFFSET) | |
1469 | left = (s64)MAX_JIFFY_OFFSET; | |
1470 | if (left < (s64)0) | |
1471 | break; | |
1472 | ||
4eb9af2a | 1473 | schedule_timeout_interruptible(left); |
1da177e4 | 1474 | |
1da177e4 LT |
1475 | left = rq_time - get_jiffies_64(); |
1476 | } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING)); | |
1477 | ||
1478 | if (abs_wqueue.task_list.next) | |
1479 | finish_wait(&nanosleep_abs_wqueue, &abs_wqueue); | |
1480 | ||
1481 | if (left > (s64)0) { | |
1482 | ||
1483 | /* | |
1484 | * Always restart abs calls from scratch to pick up any | |
1485 | * clock shifting that happened while we are away. | |
1486 | */ | |
1487 | if (abs) | |
1488 | return -ERESTARTNOHAND; | |
1489 | ||
1490 | left *= TICK_NSEC; | |
1491 | tsave->tv_sec = div_long_long_rem(left, | |
1492 | NSEC_PER_SEC, | |
1493 | &tsave->tv_nsec); | |
1494 | /* | |
1495 | * Restart works by saving the time remaing in | |
1496 | * arg2 & 3 (it is 64-bits of jiffies). The other | |
1497 | * info we need is the clock_id (saved in arg0). | |
1498 | * The sys_call interface needs the users | |
1499 | * timespec return address which _it_ saves in arg1. | |
1500 | * Since we have cast the nanosleep call to a clock_nanosleep | |
1501 | * both can be restarted with the same code. | |
1502 | */ | |
1503 | restart_block->fn = clock_nanosleep_restart; | |
1504 | restart_block->arg0 = which_clock; | |
1505 | /* | |
1506 | * Caller sets arg1 | |
1507 | */ | |
1508 | restart_block->arg2 = rq_time & 0xffffffffLL; | |
1509 | restart_block->arg3 = rq_time >> 32; | |
1510 | ||
1511 | return -ERESTART_RESTARTBLOCK; | |
1512 | } | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | /* | |
1517 | * This will restart clock_nanosleep. | |
1518 | */ | |
1519 | long | |
1520 | clock_nanosleep_restart(struct restart_block *restart_block) | |
1521 | { | |
1522 | struct timespec t; | |
1523 | int ret = common_nsleep(restart_block->arg0, 0, &t); | |
1524 | ||
1525 | if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 && | |
1526 | copy_to_user((struct timespec __user *)(restart_block->arg1), &t, | |
1527 | sizeof (t))) | |
1528 | return -EFAULT; | |
1529 | return ret; | |
1530 | } |