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