blob: 34d75926b84322a21b18aefeca361cc54da6ac22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/kernel/posix-timers.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
35#include <linux/slab.h>
36#include <linux/time.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/list.h>
41#include <linux/init.h>
42#include <linux/compiler.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040043#include <linux/hash.h>
Richard Cochran0606f422011-02-01 13:52:35 +000044#include <linux/posix-clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/posix-timers.h>
46#include <linux/syscalls.h>
47#include <linux/wait.h>
48#include <linux/workqueue.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040049#include <linux/export.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040050#include <linux/hashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040053 * Management arrays for POSIX timers. Timers are now kept in static hash table
54 * with 512 entries.
55 * Timer ids are allocated by local routine, which selects proper hash head by
56 * key, constructed from current->signal address and per signal struct counter.
57 * This keeps timer ids unique per process, but now they can intersect between
58 * processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
61/*
62 * Lets keep our timers in a slab cache :-)
63 */
Christoph Lametere18b8902006-12-06 20:33:20 -080064static struct kmem_cache *posix_timers_cache;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040065
66static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
67static DEFINE_SPINLOCK(hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
71 * SIGEV values. Here we put out an error if this assumption fails.
72 */
73#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
74 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
75#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
76#endif
77
Thomas Gleixner65da5282011-02-01 13:51:01 +000078/*
79 * parisc wants ENOTSUP instead of EOPNOTSUPP
80 */
81#ifndef ENOTSUP
82# define ENANOSLEEP_NOTSUP EOPNOTSUPP
83#else
84# define ENANOSLEEP_NOTSUP ENOTSUP
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * The timer ID is turned into a timer address by idr_find().
89 * Verifying a valid ID consists of:
90 *
91 * a) checking that idr_find() returns other than -1.
92 * b) checking that the timer id matches the one in the timer itself.
93 * c) that the timer owner is in the callers thread group.
94 */
95
96/*
97 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
98 * to implement others. This structure defines the various
Richard Cochran00617482011-02-01 13:52:15 +000099 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * RESOLUTION: Clock resolution is used to round up timer and interval
102 * times, NOT to report clock times, which are reported with as
103 * much resolution as the system can muster. In some cases this
104 * resolution may depend on the underlying clock hardware and
105 * may not be quantifiable until run time, and only then is the
106 * necessary code is written. The standard says we should say
107 * something about this issue in the documentation...
108 *
Richard Cochran00617482011-02-01 13:52:15 +0000109 * FUNCTIONS: The CLOCKs structure defines possible functions to
110 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
Richard Cochran00617482011-02-01 13:52:15 +0000112 * The standard POSIX timer management code assumes the
113 * following: 1.) The k_itimer struct (sched.h) is used for
114 * the timer. 2.) The list, it_lock, it_clock, it_id and
115 * it_pid fields are not modified by timer code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
117 * Permissions: It is assumed that the clock_settime() function defined
118 * for each clock will take care of permission checks. Some
119 * clocks may be set able by any user (i.e. local process
120 * clocks) others not. Currently the only set able clock we
121 * have is CLOCK_REALTIME and its high res counter part, both of
122 * which we beg off on and pass to do_sys_settimeofday().
123 */
124
125static struct k_clock posix_clocks[MAX_CLOCKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800127/*
128 * These ones are defined below.
129 */
130static int common_nsleep(const clockid_t, int flags, struct timespec *t,
131 struct timespec __user *rmtp);
Thomas Gleixner838394f2011-02-01 13:51:58 +0000132static int common_timer_create(struct k_itimer *new_timer);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800133static void common_timer_get(struct k_itimer *, struct itimerspec *);
134static int common_timer_set(struct k_itimer *, int,
135 struct itimerspec *, struct itimerspec *);
136static int common_timer_del(struct k_itimer *timer);
137
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800138static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Namhyung Kim20f33a02010-10-20 15:57:34 -0700140static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
141
142#define lock_timer(tid, flags) \
143({ struct k_itimer *__timr; \
144 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
145 __timr; \
146})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400148static int hash(struct signal_struct *sig, unsigned int nr)
149{
150 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
151}
152
153static struct k_itimer *__posix_timers_find(struct hlist_head *head,
154 struct signal_struct *sig,
155 timer_t id)
156{
157 struct hlist_node *node;
158 struct k_itimer *timer;
159
160 hlist_for_each_entry_rcu(timer, head, t_hash) {
161 if ((timer->it_signal == sig) && (timer->it_id == id))
162 return timer;
163 }
164 return NULL;
165}
166
167static struct k_itimer *posix_timer_by_id(timer_t id)
168{
169 struct signal_struct *sig = current->signal;
170 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
171
172 return __posix_timers_find(head, sig, id);
173}
174
175static int posix_timer_add(struct k_itimer *timer)
176{
177 struct signal_struct *sig = current->signal;
178 int first_free_id = sig->posix_timer_id;
179 struct hlist_head *head;
180 int ret = -ENOENT;
181
182 do {
183 spin_lock(&hash_lock);
184 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
185 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
186 hlist_add_head_rcu(&timer->t_hash, head);
187 ret = sig->posix_timer_id;
188 }
189 if (++sig->posix_timer_id < 0)
190 sig->posix_timer_id = 0;
191 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
192 /* Loop over all possible ids completed */
193 ret = -EAGAIN;
194 spin_unlock(&hash_lock);
195 } while (ret == -ENOENT);
196 return ret;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
200{
201 spin_unlock_irqrestore(&timr->it_lock, flags);
202}
203
Thomas Gleixner42285772011-02-01 13:51:50 +0000204/* Get clock_realtime */
205static int posix_clock_realtime_get(clockid_t which_clock, struct timespec *tp)
206{
207 ktime_get_real_ts(tp);
208 return 0;
209}
210
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000211/* Set clock_realtime */
212static int posix_clock_realtime_set(const clockid_t which_clock,
213 const struct timespec *tp)
214{
215 return do_sys_settimeofday(tp, NULL);
216}
217
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000218static int posix_clock_realtime_adj(const clockid_t which_clock,
219 struct timex *t)
220{
221 return do_adjtimex(t);
222}
223
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800224/*
225 * Get monotonic time for posix timers
226 */
227static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
228{
229 ktime_get_ts(tp);
230 return 0;
231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800234 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700235 */
236static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)
237{
238 getrawmonotonic(tp);
239 return 0;
240}
241
john stultzda15cfd2009-08-19 19:13:34 -0700242
243static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec *tp)
244{
245 *tp = current_kernel_time();
246 return 0;
247}
248
249static int posix_get_monotonic_coarse(clockid_t which_clock,
250 struct timespec *tp)
251{
252 *tp = get_monotonic_coarse();
253 return 0;
254}
255
H Hartley Sweeten6622e672010-02-02 14:41:42 -0800256static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700257{
258 *tp = ktime_to_timespec(KTIME_LOW_RES);
259 return 0;
260}
John Stultz7fdd7f82011-02-15 10:52:57 -0800261
262static int posix_get_boottime(const clockid_t which_clock, struct timespec *tp)
263{
264 get_monotonic_boottime(tp);
265 return 0;
266}
267
John Stultz1ff3c962012-05-03 12:43:40 -0700268static int posix_get_tai(clockid_t which_clock, struct timespec *tp)
269{
270 timekeeping_clocktai(tp);
271 return 0;
272}
John Stultz7fdd7f82011-02-15 10:52:57 -0800273
John Stultz2d422442008-08-20 16:37:30 -0700274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Initialize everything, well, just everything in Posix clocks/timers ;)
276 */
277static __init int init_posix_timers(void)
278{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800279 struct k_clock clock_realtime = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000280 .clock_getres = hrtimer_get_res,
Thomas Gleixner42285772011-02-01 13:51:50 +0000281 .clock_get = posix_clock_realtime_get,
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000282 .clock_set = posix_clock_realtime_set,
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000283 .clock_adj = posix_clock_realtime_adj,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000284 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000285 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000286 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000287 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000288 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000289 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 };
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800291 struct k_clock clock_monotonic = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000292 .clock_getres = hrtimer_get_res,
293 .clock_get = posix_ktime_get_ts,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000294 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000295 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000296 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000297 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000298 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000299 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 };
John Stultz2d422442008-08-20 16:37:30 -0700301 struct k_clock clock_monotonic_raw = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000302 .clock_getres = hrtimer_get_res,
303 .clock_get = posix_get_monotonic_raw,
John Stultz2d422442008-08-20 16:37:30 -0700304 };
john stultzda15cfd2009-08-19 19:13:34 -0700305 struct k_clock clock_realtime_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000306 .clock_getres = posix_get_coarse_res,
307 .clock_get = posix_get_realtime_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700308 };
309 struct k_clock clock_monotonic_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000310 .clock_getres = posix_get_coarse_res,
311 .clock_get = posix_get_monotonic_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700312 };
John Stultz1ff3c962012-05-03 12:43:40 -0700313 struct k_clock clock_tai = {
314 .clock_getres = hrtimer_get_res,
315 .clock_get = posix_get_tai,
John Stultz90adda92013-01-21 17:00:11 -0800316 .nsleep = common_nsleep,
317 .nsleep_restart = hrtimer_nanosleep_restart,
318 .timer_create = common_timer_create,
319 .timer_set = common_timer_set,
320 .timer_get = common_timer_get,
321 .timer_del = common_timer_del,
John Stultz1ff3c962012-05-03 12:43:40 -0700322 };
John Stultz7fdd7f82011-02-15 10:52:57 -0800323 struct k_clock clock_boottime = {
324 .clock_getres = hrtimer_get_res,
325 .clock_get = posix_get_boottime,
326 .nsleep = common_nsleep,
327 .nsleep_restart = hrtimer_nanosleep_restart,
328 .timer_create = common_timer_create,
329 .timer_set = common_timer_set,
330 .timer_get = common_timer_get,
331 .timer_del = common_timer_del,
332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Thomas Gleixner52708732011-02-02 12:10:09 +0100334 posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
335 posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
336 posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
337 posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
338 posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
John Stultz7fdd7f82011-02-15 10:52:57 -0800339 posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
John Stultz1ff3c962012-05-03 12:43:40 -0700340 posix_timers_register_clock(CLOCK_TAI, &clock_tai);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700343 sizeof (struct k_itimer), 0, SLAB_PANIC,
344 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348__initcall(init_posix_timers);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void schedule_next_timer(struct k_itimer *timr)
351{
Roman Zippel44f21472006-03-26 01:38:06 -0800352 struct hrtimer *timer = &timr->it.real.timer;
353
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800354 if (timr->it.real.interval.tv64 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return;
356
Davide Libenzi4d672e72008-02-04 22:27:26 -0800357 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
358 timer->base->get_time(),
359 timr->it.real.interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 timr->it_overrun_last = timr->it_overrun;
362 timr->it_overrun = -1;
363 ++timr->it_requeue_pending;
Roman Zippel44f21472006-03-26 01:38:06 -0800364 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/*
368 * This function is exported for use by the signal deliver code. It is
369 * called just prior to the info block being released and passes that
370 * block to us. It's function is to update the overrun entry AND to
371 * restart the timer. It should only be called if the timer is to be
372 * restarted (i.e. we have flagged this in the sys_private entry of the
373 * info block).
374 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300375 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * we require that the it_requeue_pending flag be set.
377 */
378void do_schedule_next_timer(struct siginfo *info)
379{
380 struct k_itimer *timr;
381 unsigned long flags;
382
383 timr = lock_timer(info->si_tid, &flags);
384
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800385 if (timr && timr->it_requeue_pending == info->si_sys_private) {
386 if (timr->it_clock < 0)
387 posix_cpu_timer_schedule(timr);
388 else
389 schedule_next_timer(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Oleg Nesterov54da1172008-07-23 20:52:05 +0400391 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800392 }
393
Thomas Gleixnerb6557fb2006-02-01 03:05:09 -0800394 if (timr)
395 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Oleg Nesterovba661292008-07-23 20:52:05 +0400398int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800400 struct task_struct *task;
401 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400402 /*
403 * FIXME: if ->sigq is queued we can race with
404 * dequeue_signal()->do_schedule_next_timer().
405 *
406 * If dequeue_signal() sees the "right" value of
407 * si_sys_private it calls do_schedule_next_timer().
408 * We re-queue ->sigq and drop ->it_lock().
409 * do_schedule_next_timer() locks the timer
410 * and re-schedules it while ->sigq is pending.
411 * Not really bad, but not that we want.
412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Oleg Nesterov27af4242008-12-01 14:18:13 -0800415 rcu_read_lock();
416 task = pid_task(timr->it_pid, PIDTYPE_PID);
417 if (task) {
418 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
419 ret = send_sigqueue(timr->sigq, task, shared);
420 }
421 rcu_read_unlock();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700422 /* If we failed to send the signal the timer stops. */
423 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425EXPORT_SYMBOL_GPL(posix_timer_event);
426
427/*
428 * This function gets called when a POSIX.1b interval timer expires. It
429 * is used as a callback from the kernel internal timer. The
430 * run_timer_list code ALWAYS calls with interrupts on.
431
432 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
433 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800434static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Roman Zippel05cfb612006-03-26 01:38:12 -0800436 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800438 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800439 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Roman Zippel05cfb612006-03-26 01:38:12 -0800441 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800443
444 if (timr->it.real.interval.tv64 != 0)
445 si_private = ++timr->it_requeue_pending;
446
447 if (posix_timer_event(timr, si_private)) {
448 /*
449 * signal was not sent because of sig_ignor
450 * we will not get a call back to restart it AND
451 * it should be restarted.
452 */
453 if (timr->it.real.interval.tv64 != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000454 ktime_t now = hrtimer_cb_get_time(timer);
455
456 /*
457 * FIXME: What we really want, is to stop this
458 * timer completely and restart it in case the
459 * SIG_IGN is removed. This is a non trivial
460 * change which involves sighand locking
461 * (sigh !), which we don't want to do late in
462 * the release cycle.
463 *
464 * For now we just let timers with an interval
465 * less than a jiffie expire every jiffie to
466 * avoid softirq starvation in case of SIG_IGN
467 * and a very small interval, which would put
468 * the timer right back on the softirq pending
469 * list. By moving now ahead of time we trick
470 * hrtimer_forward() to expire the timer
471 * later, while we still maintain the overrun
472 * accuracy, but have some inconsistency in
473 * the timer_gettime() case. This is at least
474 * better than a starved softirq. A more
475 * complex fix which solves also another related
476 * inconsistency is already in the pipeline.
477 */
478#ifdef CONFIG_HIGH_RES_TIMERS
479 {
480 ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
481
482 if (timr->it.real.interval.tv64 < kj.tv64)
483 now = ktime_add(now, kj);
484 }
485#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800486 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000487 hrtimer_forward(timer, now,
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800488 timr->it.real.interval);
489 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800490 ++timr->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800494 unlock_timer(timr, flags);
495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Oleg Nesterov27af4242008-12-01 14:18:13 -0800498static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct task_struct *rtn = current->group_leader;
501
502 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800503 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700504 !same_thread_group(rtn, current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
506 return NULL;
507
508 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
509 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
510 return NULL;
511
Oleg Nesterov27af4242008-12-01 14:18:13 -0800512 return task_pid(rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Thomas Gleixner52708732011-02-02 12:10:09 +0100515void posix_timers_register_clock(const clockid_t clock_id,
516 struct k_clock *new_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 if ((unsigned) clock_id >= MAX_CLOCKS) {
Thomas Gleixner4359ac02011-02-02 11:45:23 +0100519 printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
520 clock_id);
521 return;
522 }
523
524 if (!new_clock->clock_get) {
525 printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
526 clock_id);
527 return;
528 }
529 if (!new_clock->clock_getres) {
530 printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 clock_id);
532 return;
533 }
534
535 posix_clocks[clock_id] = *new_clock;
536}
Thomas Gleixner52708732011-02-02 12:10:09 +0100537EXPORT_SYMBOL_GPL(posix_timers_register_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539static struct k_itimer * alloc_posix_timer(void)
540{
541 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800542 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!tmr)
544 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
546 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700547 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Oleg Nesterovba661292008-07-23 20:52:05 +0400549 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return tmr;
551}
552
Eric Dumazet8af08872011-05-24 11:12:58 +0200553static void k_itimer_rcu_free(struct rcu_head *head)
554{
555 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
556
557 kmem_cache_free(posix_timers_cache, tmr);
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#define IT_ID_SET 1
561#define IT_ID_NOT_SET 0
562static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
563{
564 if (it_id_set) {
565 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400566 spin_lock_irqsave(&hash_lock, flags);
567 hlist_del_rcu(&tmr->t_hash);
568 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800570 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200572 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000575static struct k_clock *clockid_to_kclock(const clockid_t id)
576{
577 if (id < 0)
Richard Cochran0606f422011-02-01 13:52:35 +0000578 return (id & CLOCKFD_MASK) == CLOCKFD ?
579 &clock_posix_dynamic : &clock_posix_cpu;
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000580
581 if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
582 return NULL;
583 return &posix_clocks[id];
584}
585
Thomas Gleixner838394f2011-02-01 13:51:58 +0000586static int common_timer_create(struct k_itimer *new_timer)
587{
588 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
589 return 0;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/* Create a POSIX.1b interval timer. */
593
Heiko Carstens362e9c02009-01-14 14:14:07 +0100594SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
595 struct sigevent __user *, timer_event_spec,
596 timer_t __user *, created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Thomas Gleixner838394f2011-02-01 13:51:58 +0000598 struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700599 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700600 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sigevent_t event;
602 int it_id_set = IT_ID_NOT_SET;
603
Thomas Gleixner838394f2011-02-01 13:51:58 +0000604 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000606 if (!kc->timer_create)
607 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 new_timer = alloc_posix_timer();
610 if (unlikely(!new_timer))
611 return -EAGAIN;
612
613 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400614 new_timer_id = posix_timer_add(new_timer);
615 if (new_timer_id < 0) {
616 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto out;
618 }
619
620 it_id_set = IT_ID_SET;
621 new_timer->it_id = (timer_t) new_timer_id;
622 new_timer->it_clock = which_clock;
623 new_timer->it_overrun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (timer_event_spec) {
626 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
627 error = -EFAULT;
628 goto out;
629 }
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700630 rcu_read_lock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800631 new_timer->it_pid = get_pid(good_sigevent(&event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700632 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800633 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 error = -EINVAL;
635 goto out;
636 }
637 } else {
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700638 event.sigev_notify = SIGEV_SIGNAL;
639 event.sigev_signo = SIGALRM;
640 event.sigev_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800641 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700644 new_timer->it_sigev_notify = event.sigev_notify;
645 new_timer->sigq->info.si_signo = event.sigev_signo;
646 new_timer->sigq->info.si_value = event.sigev_value;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700647 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700648 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700649
Andrey Vagin2b08de02010-07-20 15:23:14 -0700650 if (copy_to_user(created_timer_id,
651 &new_timer_id, sizeof (new_timer_id))) {
652 error = -EFAULT;
653 goto out;
654 }
655
Thomas Gleixner838394f2011-02-01 13:51:58 +0000656 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700657 if (error)
658 goto out;
659
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700660 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800661 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700662 list_add(&new_timer->list, &current->signal->posix_timers);
663 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700664
665 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000666 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * In the case of the timer belonging to another task, after
668 * the task is unlocked, the timer is owned by the other task
669 * and may cease to exist at any time. Don't use or modify
670 * new_timer after the unlock call.
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700673 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return error;
675}
676
677/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * Locking issues: We need to protect the result of the id look up until
679 * we get the timer locked down so it is not deleted under us. The
680 * removal is done under the idr spinlock so we use that here to bridge
681 * the find to the timer lock. To avoid a dead lock, the timer id MUST
682 * be release with out holding the timer lock.
683 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700684static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200687
Tejun Heoe182bb32013-02-20 15:24:12 -0800688 /*
689 * timer_t could be any type >= int and we want to make sure any
690 * @timer_id outside positive int range fails lookup.
691 */
692 if ((unsigned long long)timer_id > INT_MAX)
693 return NULL;
694
Eric Dumazet8af08872011-05-24 11:12:58 +0200695 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400696 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200698 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800699 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200700 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700701 return timr;
702 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200703 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700704 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200705 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Oleg Nesterov31d92842008-09-22 14:42:51 -0700707 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * Get the time remaining on a POSIX.1b interval timer. This function
712 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
713 * mess with irq.
714 *
715 * We have a couple of messes to clean up here. First there is the case
716 * of a timer that has a requeue pending. These timers should appear to
717 * be in the timer list with an expiry as if we were to requeue them
718 * now.
719 *
720 * The second issue is the SIGEV_NONE timer which may be active but is
721 * not really ever put in the timer list (to save system resources).
722 * This timer may be expired, and if so, we will do it here. Otherwise
723 * it is the same as a requeue pending timer WRT to what we should
724 * report.
725 */
726static void
727common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
728{
Roman Zippel3b98a532006-03-26 01:38:07 -0800729 ktime_t now, remaining, iv;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800730 struct hrtimer *timer = &timr->it.real.timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800732 memset(cur_setting, 0, sizeof(struct itimerspec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Roman Zippel3b98a532006-03-26 01:38:07 -0800734 iv = timr->it.real.interval;
735
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800736 /* interval timer ? */
Roman Zippel3b98a532006-03-26 01:38:07 -0800737 if (iv.tv64)
738 cur_setting->it_interval = ktime_to_timespec(iv);
739 else if (!hrtimer_active(timer) &&
740 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800741 return;
Roman Zippel3b98a532006-03-26 01:38:07 -0800742
743 now = timer->base->get_time();
744
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800745 /*
Roman Zippel3b98a532006-03-26 01:38:07 -0800746 * When a requeue is pending or this is a SIGEV_NONE
747 * timer move the expiry time forward by intervals, so
748 * expiry is > now.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800749 */
Roman Zippel3b98a532006-03-26 01:38:07 -0800750 if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
751 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800752 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
Roman Zippel3b98a532006-03-26 01:38:07 -0800753
Arjan van de Vencc584b22008-09-01 15:02:30 -0700754 remaining = ktime_sub(hrtimer_get_expires(timer), now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800755 /* Return 0 only, when the timer is expired and not pending */
Roman Zippel3b98a532006-03-26 01:38:07 -0800756 if (remaining.tv64 <= 0) {
757 /*
758 * A single shot SIGEV_NONE timer must return 0, when
759 * it is expired !
760 */
761 if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
762 cur_setting->it_value.tv_nsec = 1;
763 } else
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800764 cur_setting->it_value = ktime_to_timespec(remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/* Get the time remaining on a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100768SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
769 struct itimerspec __user *, setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct itimerspec cur_setting;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000772 struct k_itimer *timr;
773 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000775 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 timr = lock_timer(timer_id, &flags);
778 if (!timr)
779 return -EINVAL;
780
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000781 kc = clockid_to_kclock(timr->it_clock);
782 if (WARN_ON_ONCE(!kc || !kc->timer_get))
783 ret = -EINVAL;
784 else
785 kc->timer_get(timr, &cur_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 unlock_timer(timr, flags);
788
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000789 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EFAULT;
791
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000792 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/*
796 * Get the number of overruns of a POSIX.1b interval timer. This is to
797 * be the overrun of the timer last delivered. At the same time we are
798 * accumulating overruns on the next timer. The overrun is frozen when
799 * the signal is delivered, either at the notify time (if the info block
800 * is not queued) or at the actual delivery time (as we are informed by
801 * the call back to do_schedule_next_timer(). So all we need to do is
802 * to pick up the frozen overrun.
803 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100804SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct k_itimer *timr;
807 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100808 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 timr = lock_timer(timer_id, &flags);
811 if (!timr)
812 return -EINVAL;
813
814 overrun = timr->it_overrun_last;
815 unlock_timer(timr, flags);
816
817 return overrun;
818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820/* Set a POSIX.1b interval timer. */
821/* timr->it_lock is taken. */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800822static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823common_timer_set(struct k_itimer *timr, int flags,
824 struct itimerspec *new_setting, struct itimerspec *old_setting)
825{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800826 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger79786722006-02-01 03:05:11 -0800827 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (old_setting)
830 common_timer_get(timr, old_setting);
831
832 /* disable the timer */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800833 timr->it.real.interval.tv64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
835 * careful here. If smp we could be in the "fire" routine which will
836 * be spinning as we hold the lock. But this is ONLY an SMP issue.
837 */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800838 if (hrtimer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
842 ~REQUEUE_PENDING;
843 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800844
845 /* switch off the timer when it_value is zero */
846 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800849 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
George Anzinger79786722006-02-01 03:05:11 -0800850 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
George Anzinger79786722006-02-01 03:05:11 -0800851 timr->it.real.timer.function = posix_timer_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Arjan van de Vencc584b22008-09-01 15:02:30 -0700853 hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800854
855 /* Convert interval */
856 timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
857
858 /* SIGEV_NONE timers are not queued ! See common_timer_get */
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800859 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
860 /* Setup correct expiry time for relative timers */
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100861 if (mode == HRTIMER_MODE_REL) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700862 hrtimer_add_expires(timer, timer->base->get_time());
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100863 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800864 return 0;
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800865 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800866
Arjan van de Vencc584b22008-09-01 15:02:30 -0700867 hrtimer_start_expires(timer, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
871/* Set a POSIX.1b interval timer */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100872SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
873 const struct itimerspec __user *, new_setting,
874 struct itimerspec __user *, old_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct k_itimer *timr;
877 struct itimerspec new_spec, old_spec;
878 int error = 0;
Al Viro5ba25332007-10-14 19:35:50 +0100879 unsigned long flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000881 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (!new_setting)
884 return -EINVAL;
885
886 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
887 return -EFAULT;
888
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800889 if (!timespec_valid(&new_spec.it_interval) ||
890 !timespec_valid(&new_spec.it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892retry:
893 timr = lock_timer(timer_id, &flag);
894 if (!timr)
895 return -EINVAL;
896
Thomas Gleixner27722df2011-02-01 13:52:01 +0000897 kc = clockid_to_kclock(timr->it_clock);
898 if (WARN_ON_ONCE(!kc || !kc->timer_set))
899 error = -EINVAL;
900 else
901 error = kc->timer_set(timr, flags, &new_spec, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 unlock_timer(timr, flag);
904 if (error == TIMER_RETRY) {
905 rtn = NULL; // We already got the old time...
906 goto retry;
907 }
908
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800909 if (old_setting && !error &&
910 copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 error = -EFAULT;
912
913 return error;
914}
915
Thomas Gleixner6761c672011-02-01 13:52:07 +0000916static int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800918 timer->it.real.interval.tv64 = 0;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700919
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800920 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
923}
924
925static inline int timer_delete_hook(struct k_itimer *timer)
926{
Thomas Gleixner6761c672011-02-01 13:52:07 +0000927 struct k_clock *kc = clockid_to_kclock(timer->it_clock);
928
929 if (WARN_ON_ONCE(!kc || !kc->timer_del))
930 return -EINVAL;
931 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100935SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100938 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 timer = lock_timer(timer_id, &flags);
942 if (!timer)
943 return -EINVAL;
944
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800945 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 unlock_timer(timer, flags);
947 goto retry_delete;
948 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 spin_lock(&current->sighand->siglock);
951 list_del(&timer->list);
952 spin_unlock(&current->sighand->siglock);
953 /*
954 * This keeps any tasks waiting on the spin lock from thinking
955 * they got something (see the lock code above).
956 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800957 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unlock_timer(timer, flags);
960 release_posix_timer(timer, IT_ID_SET);
961 return 0;
962}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * return timer owned by the process, used by exit_itimers
966 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800967static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 unsigned long flags;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_lock_irqsave(&timer->it_lock, flags);
973
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800974 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unlock_timer(timer, flags);
976 goto retry_delete;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 list_del(&timer->list);
979 /*
980 * This keeps any tasks waiting on the spin lock from thinking
981 * they got something (see the lock code above).
982 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800983 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 unlock_timer(timer, flags);
986 release_posix_timer(timer, IT_ID_SET);
987}
988
989/*
Roland McGrath25f407f2005-10-21 15:03:29 -0700990 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * references to the shared signal_struct.
992 */
993void exit_itimers(struct signal_struct *sig)
994{
995 struct k_itimer *tmr;
996
997 while (!list_empty(&sig->posix_timers)) {
998 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
999 itimer_delete(tmr);
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Heiko Carstens362e9c02009-01-14 14:14:07 +01001003SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1004 const struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001006 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct timespec new_tp;
1008
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001009 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1013 return -EFAULT;
1014
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001015 return kc->clock_set(which_clock, &new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Heiko Carstens362e9c02009-01-14 14:14:07 +01001018SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1019 struct timespec __user *,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Thomas Gleixner42285772011-02-01 13:51:50 +00001021 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct timespec kernel_tp;
1023 int error;
1024
Thomas Gleixner42285772011-02-01 13:51:50 +00001025 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001027
1028 error = kc->clock_get(which_clock, &kernel_tp);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1031 error = -EFAULT;
1032
1033 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001036SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1037 struct timex __user *, utx)
1038{
1039 struct k_clock *kc = clockid_to_kclock(which_clock);
1040 struct timex ktx;
1041 int err;
1042
1043 if (!kc)
1044 return -EINVAL;
1045 if (!kc->clock_adj)
1046 return -EOPNOTSUPP;
1047
1048 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1049 return -EFAULT;
1050
1051 err = kc->clock_adj(which_clock, &ktx);
1052
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001053 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001054 return -EFAULT;
1055
1056 return err;
1057}
1058
Heiko Carstens362e9c02009-01-14 14:14:07 +01001059SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1060 struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001062 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct timespec rtn_tp;
1064 int error;
1065
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001066 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EINVAL;
1068
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001069 error = kc->clock_getres(which_clock, &rtn_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001071 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 return error;
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001078 * nanosleep for monotonic and realtime clocks
1079 */
1080static int common_nsleep(const clockid_t which_clock, int flags,
1081 struct timespec *tsave, struct timespec __user *rmtp)
1082{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001083 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
1084 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1085 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Heiko Carstens362e9c02009-01-14 14:14:07 +01001088SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1089 const struct timespec __user *, rqtp,
1090 struct timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001092 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001095 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001097 if (!kc->nsleep)
1098 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1101 return -EFAULT;
1102
Thomas Gleixner5f82b2b2006-01-09 20:52:29 -08001103 if (!timespec_valid(&t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -EINVAL;
1105
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001106 return kc->nsleep(which_clock, flags, &t, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
Toyo Abe1711ef32006-09-29 02:00:28 -07001108
1109/*
Toyo Abe1711ef32006-09-29 02:00:28 -07001110 * This will restart clock_nanosleep. This is required only by
1111 * compat_clock_nanosleep_restart for now.
1112 */
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001113long clock_nanosleep_restart(struct restart_block *restart_block)
Toyo Abe1711ef32006-09-29 02:00:28 -07001114{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001115 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001116 struct k_clock *kc = clockid_to_kclock(which_clock);
Toyo Abe1711ef32006-09-29 02:00:28 -07001117
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001118 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
1119 return -EINVAL;
1120
1121 return kc->nsleep_restart(restart_block);
Toyo Abe1711ef32006-09-29 02:00:28 -07001122}