blob: 7742da826f02700fb811ea60a1d802304c515ab4 [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>
Ingo Molnar61855b62017-02-05 14:35:41 +010038#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/list.h>
42#include <linux/init.h>
43#include <linux/compiler.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040044#include <linux/hash.h>
Richard Cochran0606f422011-02-01 13:52:35 +000045#include <linux/posix-clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/posix-timers.h>
47#include <linux/syscalls.h>
48#include <linux/wait.h>
49#include <linux/workqueue.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040050#include <linux/export.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040051#include <linux/hashtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000053#include "timekeeping.h"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040056 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
64/*
65 * Lets keep our timers in a slab cache :-)
66 */
Christoph Lametere18b8902006-12-06 20:33:20 -080067static struct kmem_cache *posix_timers_cache;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040068
69static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
70static DEFINE_SPINLOCK(hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * 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
Thomas Gleixner65da5282011-02-01 13:51:01 +000081/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
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
Richard Cochran00617482011-02-01 13:52:15 +0000102 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
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 *
Richard Cochran00617482011-02-01 13:52:15 +0000112 * FUNCTIONS: The CLOCKs structure defines possible functions to
113 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Richard Cochran00617482011-02-01 13:52:15 +0000115 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800130/*
131 * These ones are defined below.
132 */
133static int common_nsleep(const clockid_t, int flags, struct timespec *t,
134 struct timespec __user *rmtp);
Thomas Gleixner838394f2011-02-01 13:51:58 +0000135static int common_timer_create(struct k_itimer *new_timer);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800136static 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);
140
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800141static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Namhyung Kim20f33a02010-10-20 15:57:34 -0700143static 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})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400151static 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{
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400160 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
202{
203 spin_unlock_irqrestore(&timr->it_lock, flags);
204}
205
Thomas Gleixner42285772011-02-01 13:51:50 +0000206/* Get clock_realtime */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700207static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner42285772011-02-01 13:51:50 +0000208{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700209 ktime_get_real_ts64(tp);
Thomas Gleixner42285772011-02-01 13:51:50 +0000210 return 0;
211}
212
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000213/* Set clock_realtime */
214static int posix_clock_realtime_set(const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700215 const struct timespec64 *tp)
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000216{
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700217 return do_sys_settimeofday64(tp, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000218}
219
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000220static int posix_clock_realtime_adj(const clockid_t which_clock,
221 struct timex *t)
222{
223 return do_adjtimex(t);
224}
225
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800226/*
227 * Get monotonic time for posix timers
228 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700229static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800230{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700231 ktime_get_ts64(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800232 return 0;
233}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800236 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700237 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700238static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700239{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700240 getrawmonotonic64(tp);
John Stultz2d422442008-08-20 16:37:30 -0700241 return 0;
242}
243
john stultzda15cfd2009-08-19 19:13:34 -0700244
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700245static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700246{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700247 *tp = current_kernel_time64();
john stultzda15cfd2009-08-19 19:13:34 -0700248 return 0;
249}
250
251static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700252 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700253{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700254 *tp = get_monotonic_coarse64();
john stultzda15cfd2009-08-19 19:13:34 -0700255 return 0;
256}
257
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700258static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700259{
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700260 *tp = ktime_to_timespec64(KTIME_LOW_RES);
john stultzda15cfd2009-08-19 19:13:34 -0700261 return 0;
262}
John Stultz7fdd7f82011-02-15 10:52:57 -0800263
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700264static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
John Stultz7fdd7f82011-02-15 10:52:57 -0800265{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700266 get_monotonic_boottime64(tp);
John Stultz7fdd7f82011-02-15 10:52:57 -0800267 return 0;
268}
269
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700270static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700271{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700272 timekeeping_clocktai64(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700273 return 0;
274}
John Stultz7fdd7f82011-02-15 10:52:57 -0800275
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700276static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000277{
278 tp->tv_sec = 0;
279 tp->tv_nsec = hrtimer_resolution;
280 return 0;
281}
282
John Stultz2d422442008-08-20 16:37:30 -0700283/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * Initialize everything, well, just everything in Posix clocks/timers ;)
285 */
286static __init int init_posix_timers(void)
287{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800288 struct k_clock clock_realtime = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000289 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner42285772011-02-01 13:51:50 +0000290 .clock_get = posix_clock_realtime_get,
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000291 .clock_set = posix_clock_realtime_set,
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000292 .clock_adj = posix_clock_realtime_adj,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000293 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000294 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000295 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000296 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000297 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000298 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 };
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800300 struct k_clock clock_monotonic = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000301 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000302 .clock_get = posix_ktime_get_ts,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000303 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000304 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000305 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000306 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000307 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000308 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 };
John Stultz2d422442008-08-20 16:37:30 -0700310 struct k_clock clock_monotonic_raw = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000311 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000312 .clock_get = posix_get_monotonic_raw,
John Stultz2d422442008-08-20 16:37:30 -0700313 };
john stultzda15cfd2009-08-19 19:13:34 -0700314 struct k_clock clock_realtime_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000315 .clock_getres = posix_get_coarse_res,
316 .clock_get = posix_get_realtime_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700317 };
318 struct k_clock clock_monotonic_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000319 .clock_getres = posix_get_coarse_res,
320 .clock_get = posix_get_monotonic_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700321 };
John Stultz1ff3c962012-05-03 12:43:40 -0700322 struct k_clock clock_tai = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000323 .clock_getres = posix_get_hrtimer_res,
John Stultz1ff3c962012-05-03 12:43:40 -0700324 .clock_get = posix_get_tai,
John Stultz90adda92013-01-21 17:00:11 -0800325 .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,
John Stultz1ff3c962012-05-03 12:43:40 -0700331 };
John Stultz7fdd7f82011-02-15 10:52:57 -0800332 struct k_clock clock_boottime = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000333 .clock_getres = posix_get_hrtimer_res,
John Stultz7fdd7f82011-02-15 10:52:57 -0800334 .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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Thomas Gleixner52708732011-02-02 12:10:09 +0100343 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);
John Stultz7fdd7f82011-02-15 10:52:57 -0800348 posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
John Stultz1ff3c962012-05-03 12:43:40 -0700349 posix_timers_register_clock(CLOCK_TAI, &clock_tai);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700352 sizeof (struct k_itimer), 0, SLAB_PANIC,
353 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
357__initcall(init_posix_timers);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static void schedule_next_timer(struct k_itimer *timr)
360{
Roman Zippel44f21472006-03-26 01:38:06 -0800361 struct hrtimer *timer = &timr->it.real.timer;
362
Thomas Gleixner2456e852016-12-25 11:38:40 +0100363 if (timr->it.real.interval == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return;
365
Davide Libenzi4d672e72008-02-04 22:27:26 -0800366 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
367 timer->base->get_time(),
368 timr->it.real.interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 timr->it_overrun_last = timr->it_overrun;
371 timr->it_overrun = -1;
372 ++timr->it_requeue_pending;
Roman Zippel44f21472006-03-26 01:38:06 -0800373 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
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 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300384 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * 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
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800394 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Oleg Nesterov54da1172008-07-23 20:52:05 +0400400 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800401 }
402
Thomas Gleixnerb6557fb2006-02-01 03:05:09 -0800403 if (timr)
404 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Oleg Nesterovba661292008-07-23 20:52:05 +0400407int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800409 struct task_struct *task;
410 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400411 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Oleg Nesterov27af4242008-12-01 14:18:13 -0800424 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();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700431 /* If we failed to send the signal the timer stops. */
432 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
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 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800443static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Roman Zippel05cfb612006-03-26 01:38:12 -0800445 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800447 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800448 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Roman Zippel05cfb612006-03-26 01:38:12 -0800450 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800452
Thomas Gleixner2456e852016-12-25 11:38:40 +0100453 if (timr->it.real.interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800454 si_private = ++timr->it_requeue_pending;
455
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 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100462 if (timr->it.real.interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000463 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 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100489 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000490
Thomas Gleixner2456e852016-12-25 11:38:40 +0100491 if (timr->it.real.interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000492 now = ktime_add(now, kj);
493 }
494#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800495 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000496 hrtimer_forward(timer, now,
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800497 timr->it.real.interval);
498 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800499 ++timr->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800503 unlock_timer(timr, flags);
504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Oleg Nesterov27af4242008-12-01 14:18:13 -0800507static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct task_struct *rtn = current->group_leader;
510
511 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800512 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700513 !same_thread_group(rtn, current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 (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
Oleg Nesterov27af4242008-12-01 14:18:13 -0800521 return task_pid(rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Thomas Gleixner52708732011-02-02 12:10:09 +0100524void posix_timers_register_clock(const clockid_t clock_id,
525 struct k_clock *new_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 if ((unsigned) clock_id >= MAX_CLOCKS) {
Thomas Gleixner4359ac02011-02-02 11:45:23 +0100528 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",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 clock_id);
541 return;
542 }
543
544 posix_clocks[clock_id] = *new_clock;
545}
Thomas Gleixner52708732011-02-02 12:10:09 +0100546EXPORT_SYMBOL_GPL(posix_timers_register_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548static struct k_itimer * alloc_posix_timer(void)
549{
550 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800551 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!tmr)
553 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
555 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700556 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Oleg Nesterovba661292008-07-23 20:52:05 +0400558 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return tmr;
560}
561
Eric Dumazet8af08872011-05-24 11:12:58 +0200562static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#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;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400575 spin_lock_irqsave(&hash_lock, flags);
576 hlist_del_rcu(&tmr->t_hash);
577 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800579 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200581 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000584static struct k_clock *clockid_to_kclock(const clockid_t id)
585{
586 if (id < 0)
Richard Cochran0606f422011-02-01 13:52:35 +0000587 return (id & CLOCKFD_MASK) == CLOCKFD ?
588 &clock_posix_dynamic : &clock_posix_cpu;
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000589
590 if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
591 return NULL;
592 return &posix_clocks[id];
593}
594
Thomas Gleixner838394f2011-02-01 13:51:58 +0000595static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/* Create a POSIX.1b interval timer. */
602
Heiko Carstens362e9c02009-01-14 14:14:07 +0100603SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
604 struct sigevent __user *, timer_event_spec,
605 timer_t __user *, created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Thomas Gleixner838394f2011-02-01 13:51:58 +0000607 struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700608 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700609 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sigevent_t event;
611 int it_id_set = IT_ID_NOT_SET;
612
Thomas Gleixner838394f2011-02-01 13:51:58 +0000613 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000615 if (!kc->timer_create)
616 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 new_timer = alloc_posix_timer();
619 if (unlikely(!new_timer))
620 return -EAGAIN;
621
622 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400623 new_timer_id = posix_timer_add(new_timer);
624 if (new_timer_id < 0) {
625 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (timer_event_spec) {
635 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
636 error = -EFAULT;
637 goto out;
638 }
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700639 rcu_read_lock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800640 new_timer->it_pid = get_pid(good_sigevent(&event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700641 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800642 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 error = -EINVAL;
644 goto out;
645 }
646 } else {
Mathias Krause6891c452014-10-04 23:06:39 +0200647 memset(&event.sigev_value, 0, sizeof(event.sigev_value));
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700648 event.sigev_notify = SIGEV_SIGNAL;
649 event.sigev_signo = SIGALRM;
650 event.sigev_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800651 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700654 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;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700657 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700658 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700659
Andrey Vagin2b08de02010-07-20 15:23:14 -0700660 if (copy_to_user(created_timer_id,
661 &new_timer_id, sizeof (new_timer_id))) {
662 error = -EFAULT;
663 goto out;
664 }
665
Thomas Gleixner838394f2011-02-01 13:51:58 +0000666 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700667 if (error)
668 goto out;
669
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700670 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800671 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700672 list_add(&new_timer->list, &current->signal->posix_timers);
673 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700674
675 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000676 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700683 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
685}
686
687/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * 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 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700694static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200697
Tejun Heoe182bb32013-02-20 15:24:12 -0800698 /*
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
Eric Dumazet8af08872011-05-24 11:12:58 +0200705 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400706 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200708 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800709 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200710 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700711 return timr;
712 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200713 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700714 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200715 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Oleg Nesterov31d92842008-09-22 14:42:51 -0700717 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
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
737common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
738{
Roman Zippel3b98a532006-03-26 01:38:07 -0800739 ktime_t now, remaining, iv;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800740 struct hrtimer *timer = &timr->it.real.timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800742 memset(cur_setting, 0, sizeof(struct itimerspec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Roman Zippel3b98a532006-03-26 01:38:07 -0800744 iv = timr->it.real.interval;
745
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800746 /* interval timer ? */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100747 if (iv)
Roman Zippel3b98a532006-03-26 01:38:07 -0800748 cur_setting->it_interval = ktime_to_timespec(iv);
749 else if (!hrtimer_active(timer) &&
750 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800751 return;
Roman Zippel3b98a532006-03-26 01:38:07 -0800752
753 now = timer->base->get_time();
754
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800755 /*
Roman Zippel3b98a532006-03-26 01:38:07 -0800756 * 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.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800759 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100760 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
761 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800762 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
Roman Zippel3b98a532006-03-26 01:38:07 -0800763
Thomas Gleixner572c3912016-01-14 16:54:47 +0000764 remaining = __hrtimer_expires_remaining_adjusted(timer, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800765 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100766 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800767 /*
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
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800774 cur_setting->it_value = ktime_to_timespec(remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/* Get the time remaining on a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100778SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
779 struct itimerspec __user *, setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct itimerspec cur_setting;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000782 struct k_itimer *timr;
783 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000785 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 timr = lock_timer(timer_id, &flags);
788 if (!timr)
789 return -EINVAL;
790
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000791 kc = clockid_to_kclock(timr->it_clock);
792 if (WARN_ON_ONCE(!kc || !kc->timer_get))
793 ret = -EINVAL;
794 else
795 kc->timer_get(timr, &cur_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 unlock_timer(timr, flags);
798
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000799 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EFAULT;
801
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000802 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * Get the number of overruns of a POSIX.1b interval timer. This is to
807 * be the overrun of the timer last delivered. At the same time we are
808 * accumulating overruns on the next timer. The overrun is frozen when
809 * the signal is delivered, either at the notify time (if the info block
810 * is not queued) or at the actual delivery time (as we are informed by
811 * the call back to do_schedule_next_timer(). So all we need to do is
812 * to pick up the frozen overrun.
813 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100814SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct k_itimer *timr;
817 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100818 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 timr = lock_timer(timer_id, &flags);
821 if (!timr)
822 return -EINVAL;
823
824 overrun = timr->it_overrun_last;
825 unlock_timer(timr, flags);
826
827 return overrun;
828}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830/* Set a POSIX.1b interval timer. */
831/* timr->it_lock is taken. */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800832static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833common_timer_set(struct k_itimer *timr, int flags,
834 struct itimerspec *new_setting, struct itimerspec *old_setting)
835{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800836 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800837 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (old_setting)
840 common_timer_get(timr, old_setting);
841
842 /* disable the timer */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100843 timr->it.real.interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * careful here. If smp we could be in the "fire" routine which will
846 * be spinning as we hold the lock. But this is ONLY an SMP issue.
847 */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800848 if (hrtimer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
852 ~REQUEUE_PENDING;
853 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800854
855 /* switch off the timer when it_value is zero */
856 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800859 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
George Anzinger7978672c2006-02-01 03:05:11 -0800860 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
George Anzinger7978672c2006-02-01 03:05:11 -0800861 timr->it.real.timer.function = posix_timer_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Arjan van de Vencc584b22008-09-01 15:02:30 -0700863 hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800864
865 /* Convert interval */
866 timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
867
868 /* SIGEV_NONE timers are not queued ! See common_timer_get */
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800869 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
870 /* Setup correct expiry time for relative timers */
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100871 if (mode == HRTIMER_MODE_REL) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700872 hrtimer_add_expires(timer, timer->base->get_time());
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100873 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800874 return 0;
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800875 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800876
Arjan van de Vencc584b22008-09-01 15:02:30 -0700877 hrtimer_start_expires(timer, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
881/* Set a POSIX.1b interval timer */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100882SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
883 const struct itimerspec __user *, new_setting,
884 struct itimerspec __user *, old_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 struct k_itimer *timr;
887 struct itimerspec new_spec, old_spec;
888 int error = 0;
Al Viro5ba25332007-10-14 19:35:50 +0100889 unsigned long flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000891 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (!new_setting)
894 return -EINVAL;
895
896 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
897 return -EFAULT;
898
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800899 if (!timespec_valid(&new_spec.it_interval) ||
900 !timespec_valid(&new_spec.it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EINVAL;
902retry:
903 timr = lock_timer(timer_id, &flag);
904 if (!timr)
905 return -EINVAL;
906
Thomas Gleixner27722df2011-02-01 13:52:01 +0000907 kc = clockid_to_kclock(timr->it_clock);
908 if (WARN_ON_ONCE(!kc || !kc->timer_set))
909 error = -EINVAL;
910 else
911 error = kc->timer_set(timr, flags, &new_spec, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 unlock_timer(timr, flag);
914 if (error == TIMER_RETRY) {
915 rtn = NULL; // We already got the old time...
916 goto retry;
917 }
918
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800919 if (old_setting && !error &&
920 copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 error = -EFAULT;
922
923 return error;
924}
925
Thomas Gleixner6761c672011-02-01 13:52:07 +0000926static int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100928 timer->it.real.interval = 0;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700929
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800930 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
934
935static inline int timer_delete_hook(struct k_itimer *timer)
936{
Thomas Gleixner6761c672011-02-01 13:52:07 +0000937 struct k_clock *kc = clockid_to_kclock(timer->it_clock);
938
939 if (WARN_ON_ONCE(!kc || !kc->timer_del))
940 return -EINVAL;
941 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100945SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100948 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 timer = lock_timer(timer_id, &flags);
952 if (!timer)
953 return -EINVAL;
954
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800955 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unlock_timer(timer, flags);
957 goto retry_delete;
958 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_lock(&current->sighand->siglock);
961 list_del(&timer->list);
962 spin_unlock(&current->sighand->siglock);
963 /*
964 * This keeps any tasks waiting on the spin lock from thinking
965 * they got something (see the lock code above).
966 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800967 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unlock_timer(timer, flags);
970 release_posix_timer(timer, IT_ID_SET);
971 return 0;
972}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/*
975 * return timer owned by the process, used by exit_itimers
976 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800977static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 unsigned long flags;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_lock_irqsave(&timer->it_lock, flags);
983
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800984 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 unlock_timer(timer, flags);
986 goto retry_delete;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 list_del(&timer->list);
989 /*
990 * This keeps any tasks waiting on the spin lock from thinking
991 * they got something (see the lock code above).
992 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800993 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unlock_timer(timer, flags);
996 release_posix_timer(timer, IT_ID_SET);
997}
998
999/*
Roland McGrath25f407f2005-10-21 15:03:29 -07001000 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * references to the shared signal_struct.
1002 */
1003void exit_itimers(struct signal_struct *sig)
1004{
1005 struct k_itimer *tmr;
1006
1007 while (!list_empty(&sig->posix_timers)) {
1008 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1009 itimer_delete(tmr);
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Heiko Carstens362e9c02009-01-14 14:14:07 +01001013SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1014 const struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001016 struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001017 struct timespec64 new_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct timespec new_tp;
1019
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001020 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1024 return -EFAULT;
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001025 new_tp64 = timespec_to_timespec64(new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001027 return kc->clock_set(which_clock, &new_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Heiko Carstens362e9c02009-01-14 14:14:07 +01001030SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1031 struct timespec __user *,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Thomas Gleixner42285772011-02-01 13:51:50 +00001033 struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001034 struct timespec64 kernel_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct timespec kernel_tp;
1036 int error;
1037
Thomas Gleixner42285772011-02-01 13:51:50 +00001038 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001040
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001041 error = kc->clock_get(which_clock, &kernel_tp64);
1042 kernel_tp = timespec64_to_timespec(kernel_tp64);
Thomas Gleixner42285772011-02-01 13:51:50 +00001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1045 error = -EFAULT;
1046
1047 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001050SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1051 struct timex __user *, utx)
1052{
1053 struct k_clock *kc = clockid_to_kclock(which_clock);
1054 struct timex ktx;
1055 int err;
1056
1057 if (!kc)
1058 return -EINVAL;
1059 if (!kc->clock_adj)
1060 return -EOPNOTSUPP;
1061
1062 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1063 return -EFAULT;
1064
1065 err = kc->clock_adj(which_clock, &ktx);
1066
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001067 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001068 return -EFAULT;
1069
1070 return err;
1071}
1072
Heiko Carstens362e9c02009-01-14 14:14:07 +01001073SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1074 struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001076 struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001077 struct timespec64 rtn_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct timespec rtn_tp;
1079 int error;
1080
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001081 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EINVAL;
1083
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001084 error = kc->clock_getres(which_clock, &rtn_tp64);
1085 rtn_tp = timespec64_to_timespec(rtn_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001087 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 return error;
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001094 * nanosleep for monotonic and realtime clocks
1095 */
1096static int common_nsleep(const clockid_t which_clock, int flags,
1097 struct timespec *tsave, struct timespec __user *rmtp)
1098{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001099 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
1100 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1101 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Heiko Carstens362e9c02009-01-14 14:14:07 +01001104SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1105 const struct timespec __user *, rqtp,
1106 struct timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001108 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001111 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001113 if (!kc->nsleep)
1114 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1117 return -EFAULT;
1118
Thomas Gleixner5f82b2b2006-01-09 20:52:29 -08001119 if (!timespec_valid(&t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -EINVAL;
1121
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001122 return kc->nsleep(which_clock, flags, &t, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
Toyo Abe1711ef32006-09-29 02:00:28 -07001124
1125/*
Toyo Abe1711ef32006-09-29 02:00:28 -07001126 * This will restart clock_nanosleep. This is required only by
1127 * compat_clock_nanosleep_restart for now.
1128 */
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001129long clock_nanosleep_restart(struct restart_block *restart_block)
Toyo Abe1711ef32006-09-29 02:00:28 -07001130{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001131 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001132 struct k_clock *kc = clockid_to_kclock(which_clock);
Toyo Abe1711ef32006-09-29 02:00:28 -07001133
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001134 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
1135 return -EINVAL;
1136
1137 return kc->nsleep_restart(restart_block);
Toyo Abe1711ef32006-09-29 02:00:28 -07001138}