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