blob: 68170642c77ca1e46d96a2dcafcbf3a81d58f5c1 [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,
215 const struct timespec *tp)
216{
Deepa Dinamani2ac00f12017-03-26 12:04:12 -0700217 struct timespec64 tp64;
218
219 tp64 = timespec_to_timespec64(*tp);
220 return do_sys_settimeofday64(&tp64, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000221}
222
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000223static int posix_clock_realtime_adj(const clockid_t which_clock,
224 struct timex *t)
225{
226 return do_adjtimex(t);
227}
228
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800229/*
230 * Get monotonic time for posix timers
231 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700232static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800233{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700234 ktime_get_ts64(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800235 return 0;
236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800239 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700240 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700241static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700242{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700243 getrawmonotonic64(tp);
John Stultz2d422442008-08-20 16:37:30 -0700244 return 0;
245}
246
john stultzda15cfd2009-08-19 19:13:34 -0700247
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700248static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700249{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700250 *tp = current_kernel_time64();
john stultzda15cfd2009-08-19 19:13:34 -0700251 return 0;
252}
253
254static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700255 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700256{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700257 *tp = get_monotonic_coarse64();
john stultzda15cfd2009-08-19 19:13:34 -0700258 return 0;
259}
260
H Hartley Sweeten6622e672010-02-02 14:41:42 -0800261static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700262{
263 *tp = ktime_to_timespec(KTIME_LOW_RES);
264 return 0;
265}
John Stultz7fdd7f82011-02-15 10:52:57 -0800266
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700267static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
John Stultz7fdd7f82011-02-15 10:52:57 -0800268{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700269 get_monotonic_boottime64(tp);
John Stultz7fdd7f82011-02-15 10:52:57 -0800270 return 0;
271}
272
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700273static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700274{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700275 timekeeping_clocktai64(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700276 return 0;
277}
John Stultz7fdd7f82011-02-15 10:52:57 -0800278
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000279static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec *tp)
280{
281 tp->tv_sec = 0;
282 tp->tv_nsec = hrtimer_resolution;
283 return 0;
284}
285
John Stultz2d422442008-08-20 16:37:30 -0700286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * Initialize everything, well, just everything in Posix clocks/timers ;)
288 */
289static __init int init_posix_timers(void)
290{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800291 struct k_clock clock_realtime = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000292 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner42285772011-02-01 13:51:50 +0000293 .clock_get = posix_clock_realtime_get,
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000294 .clock_set = posix_clock_realtime_set,
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000295 .clock_adj = posix_clock_realtime_adj,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000296 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000297 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000298 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000299 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000300 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000301 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 };
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800303 struct k_clock clock_monotonic = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000304 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000305 .clock_get = posix_ktime_get_ts,
Thomas Gleixnera5cd2882011-02-01 13:51:11 +0000306 .nsleep = common_nsleep,
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +0000307 .nsleep_restart = hrtimer_nanosleep_restart,
Thomas Gleixner838394f2011-02-01 13:51:58 +0000308 .timer_create = common_timer_create,
Thomas Gleixner27722df2011-02-01 13:52:01 +0000309 .timer_set = common_timer_set,
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000310 .timer_get = common_timer_get,
Thomas Gleixner6761c672011-02-01 13:52:07 +0000311 .timer_del = common_timer_del,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 };
John Stultz2d422442008-08-20 16:37:30 -0700313 struct k_clock clock_monotonic_raw = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000314 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000315 .clock_get = posix_get_monotonic_raw,
John Stultz2d422442008-08-20 16:37:30 -0700316 };
john stultzda15cfd2009-08-19 19:13:34 -0700317 struct k_clock clock_realtime_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000318 .clock_getres = posix_get_coarse_res,
319 .clock_get = posix_get_realtime_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700320 };
321 struct k_clock clock_monotonic_coarse = {
Thomas Gleixner2fd1f042011-02-01 13:51:03 +0000322 .clock_getres = posix_get_coarse_res,
323 .clock_get = posix_get_monotonic_coarse,
john stultzda15cfd2009-08-19 19:13:34 -0700324 };
John Stultz1ff3c962012-05-03 12:43:40 -0700325 struct k_clock clock_tai = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000326 .clock_getres = posix_get_hrtimer_res,
John Stultz1ff3c962012-05-03 12:43:40 -0700327 .clock_get = posix_get_tai,
John Stultz90adda92013-01-21 17:00:11 -0800328 .nsleep = common_nsleep,
329 .nsleep_restart = hrtimer_nanosleep_restart,
330 .timer_create = common_timer_create,
331 .timer_set = common_timer_set,
332 .timer_get = common_timer_get,
333 .timer_del = common_timer_del,
John Stultz1ff3c962012-05-03 12:43:40 -0700334 };
John Stultz7fdd7f82011-02-15 10:52:57 -0800335 struct k_clock clock_boottime = {
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000336 .clock_getres = posix_get_hrtimer_res,
John Stultz7fdd7f82011-02-15 10:52:57 -0800337 .clock_get = posix_get_boottime,
338 .nsleep = common_nsleep,
339 .nsleep_restart = hrtimer_nanosleep_restart,
340 .timer_create = common_timer_create,
341 .timer_set = common_timer_set,
342 .timer_get = common_timer_get,
343 .timer_del = common_timer_del,
344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Thomas Gleixner52708732011-02-02 12:10:09 +0100346 posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
347 posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
348 posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
349 posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
350 posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
John Stultz7fdd7f82011-02-15 10:52:57 -0800351 posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
John Stultz1ff3c962012-05-03 12:43:40 -0700352 posix_timers_register_clock(CLOCK_TAI, &clock_tai);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700355 sizeof (struct k_itimer), 0, SLAB_PANIC,
356 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360__initcall(init_posix_timers);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void schedule_next_timer(struct k_itimer *timr)
363{
Roman Zippel44f21472006-03-26 01:38:06 -0800364 struct hrtimer *timer = &timr->it.real.timer;
365
Thomas Gleixner2456e852016-12-25 11:38:40 +0100366 if (timr->it.real.interval == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
368
Davide Libenzi4d672e72008-02-04 22:27:26 -0800369 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
370 timer->base->get_time(),
371 timr->it.real.interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 timr->it_overrun_last = timr->it_overrun;
374 timr->it_overrun = -1;
375 ++timr->it_requeue_pending;
Roman Zippel44f21472006-03-26 01:38:06 -0800376 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/*
380 * This function is exported for use by the signal deliver code. It is
381 * called just prior to the info block being released and passes that
382 * block to us. It's function is to update the overrun entry AND to
383 * restart the timer. It should only be called if the timer is to be
384 * restarted (i.e. we have flagged this in the sys_private entry of the
385 * info block).
386 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300387 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * we require that the it_requeue_pending flag be set.
389 */
390void do_schedule_next_timer(struct siginfo *info)
391{
392 struct k_itimer *timr;
393 unsigned long flags;
394
395 timr = lock_timer(info->si_tid, &flags);
396
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800397 if (timr && timr->it_requeue_pending == info->si_sys_private) {
398 if (timr->it_clock < 0)
399 posix_cpu_timer_schedule(timr);
400 else
401 schedule_next_timer(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Oleg Nesterov54da1172008-07-23 20:52:05 +0400403 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800404 }
405
Thomas Gleixnerb6557fb2006-02-01 03:05:09 -0800406 if (timr)
407 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Oleg Nesterovba661292008-07-23 20:52:05 +0400410int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800412 struct task_struct *task;
413 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400414 /*
415 * FIXME: if ->sigq is queued we can race with
416 * dequeue_signal()->do_schedule_next_timer().
417 *
418 * If dequeue_signal() sees the "right" value of
419 * si_sys_private it calls do_schedule_next_timer().
420 * We re-queue ->sigq and drop ->it_lock().
421 * do_schedule_next_timer() locks the timer
422 * and re-schedules it while ->sigq is pending.
423 * Not really bad, but not that we want.
424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Oleg Nesterov27af4242008-12-01 14:18:13 -0800427 rcu_read_lock();
428 task = pid_task(timr->it_pid, PIDTYPE_PID);
429 if (task) {
430 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
431 ret = send_sigqueue(timr->sigq, task, shared);
432 }
433 rcu_read_unlock();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700434 /* If we failed to send the signal the timer stops. */
435 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437EXPORT_SYMBOL_GPL(posix_timer_event);
438
439/*
440 * This function gets called when a POSIX.1b interval timer expires. It
441 * is used as a callback from the kernel internal timer. The
442 * run_timer_list code ALWAYS calls with interrupts on.
443
444 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
445 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800446static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Roman Zippel05cfb612006-03-26 01:38:12 -0800448 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800450 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800451 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Roman Zippel05cfb612006-03-26 01:38:12 -0800453 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800455
Thomas Gleixner2456e852016-12-25 11:38:40 +0100456 if (timr->it.real.interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800457 si_private = ++timr->it_requeue_pending;
458
459 if (posix_timer_event(timr, si_private)) {
460 /*
461 * signal was not sent because of sig_ignor
462 * we will not get a call back to restart it AND
463 * it should be restarted.
464 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100465 if (timr->it.real.interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000466 ktime_t now = hrtimer_cb_get_time(timer);
467
468 /*
469 * FIXME: What we really want, is to stop this
470 * timer completely and restart it in case the
471 * SIG_IGN is removed. This is a non trivial
472 * change which involves sighand locking
473 * (sigh !), which we don't want to do late in
474 * the release cycle.
475 *
476 * For now we just let timers with an interval
477 * less than a jiffie expire every jiffie to
478 * avoid softirq starvation in case of SIG_IGN
479 * and a very small interval, which would put
480 * the timer right back on the softirq pending
481 * list. By moving now ahead of time we trick
482 * hrtimer_forward() to expire the timer
483 * later, while we still maintain the overrun
484 * accuracy, but have some inconsistency in
485 * the timer_gettime() case. This is at least
486 * better than a starved softirq. A more
487 * complex fix which solves also another related
488 * inconsistency is already in the pipeline.
489 */
490#ifdef CONFIG_HIGH_RES_TIMERS
491 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100492 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000493
Thomas Gleixner2456e852016-12-25 11:38:40 +0100494 if (timr->it.real.interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000495 now = ktime_add(now, kj);
496 }
497#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800498 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000499 hrtimer_forward(timer, now,
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800500 timr->it.real.interval);
501 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800502 ++timr->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800506 unlock_timer(timr, flags);
507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Oleg Nesterov27af4242008-12-01 14:18:13 -0800510static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct task_struct *rtn = current->group_leader;
513
514 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800515 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700516 !same_thread_group(rtn, current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
518 return NULL;
519
520 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
521 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
522 return NULL;
523
Oleg Nesterov27af4242008-12-01 14:18:13 -0800524 return task_pid(rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Thomas Gleixner52708732011-02-02 12:10:09 +0100527void posix_timers_register_clock(const clockid_t clock_id,
528 struct k_clock *new_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 if ((unsigned) clock_id >= MAX_CLOCKS) {
Thomas Gleixner4359ac02011-02-02 11:45:23 +0100531 printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
532 clock_id);
533 return;
534 }
535
536 if (!new_clock->clock_get) {
537 printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
538 clock_id);
539 return;
540 }
541 if (!new_clock->clock_getres) {
542 printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 clock_id);
544 return;
545 }
546
547 posix_clocks[clock_id] = *new_clock;
548}
Thomas Gleixner52708732011-02-02 12:10:09 +0100549EXPORT_SYMBOL_GPL(posix_timers_register_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551static struct k_itimer * alloc_posix_timer(void)
552{
553 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800554 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!tmr)
556 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
558 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Oleg Nesterovba661292008-07-23 20:52:05 +0400561 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return tmr;
563}
564
Eric Dumazet8af08872011-05-24 11:12:58 +0200565static void k_itimer_rcu_free(struct rcu_head *head)
566{
567 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
568
569 kmem_cache_free(posix_timers_cache, tmr);
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#define IT_ID_SET 1
573#define IT_ID_NOT_SET 0
574static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
575{
576 if (it_id_set) {
577 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400578 spin_lock_irqsave(&hash_lock, flags);
579 hlist_del_rcu(&tmr->t_hash);
580 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800582 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200584 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000587static struct k_clock *clockid_to_kclock(const clockid_t id)
588{
589 if (id < 0)
Richard Cochran0606f422011-02-01 13:52:35 +0000590 return (id & CLOCKFD_MASK) == CLOCKFD ?
591 &clock_posix_dynamic : &clock_posix_cpu;
Thomas Gleixnercc785ac2011-02-01 13:51:09 +0000592
593 if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
594 return NULL;
595 return &posix_clocks[id];
596}
597
Thomas Gleixner838394f2011-02-01 13:51:58 +0000598static int common_timer_create(struct k_itimer *new_timer)
599{
600 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
601 return 0;
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/* Create a POSIX.1b interval timer. */
605
Heiko Carstens362e9c02009-01-14 14:14:07 +0100606SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
607 struct sigevent __user *, timer_event_spec,
608 timer_t __user *, created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Thomas Gleixner838394f2011-02-01 13:51:58 +0000610 struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700611 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700612 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sigevent_t event;
614 int it_id_set = IT_ID_NOT_SET;
615
Thomas Gleixner838394f2011-02-01 13:51:58 +0000616 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000618 if (!kc->timer_create)
619 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 new_timer = alloc_posix_timer();
622 if (unlikely(!new_timer))
623 return -EAGAIN;
624
625 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400626 new_timer_id = posix_timer_add(new_timer);
627 if (new_timer_id < 0) {
628 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto out;
630 }
631
632 it_id_set = IT_ID_SET;
633 new_timer->it_id = (timer_t) new_timer_id;
634 new_timer->it_clock = which_clock;
635 new_timer->it_overrun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (timer_event_spec) {
638 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
639 error = -EFAULT;
640 goto out;
641 }
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700642 rcu_read_lock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800643 new_timer->it_pid = get_pid(good_sigevent(&event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700644 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800645 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 error = -EINVAL;
647 goto out;
648 }
649 } else {
Mathias Krause6891c452014-10-04 23:06:39 +0200650 memset(&event.sigev_value, 0, sizeof(event.sigev_value));
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700651 event.sigev_notify = SIGEV_SIGNAL;
652 event.sigev_signo = SIGALRM;
653 event.sigev_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800654 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700657 new_timer->it_sigev_notify = event.sigev_notify;
658 new_timer->sigq->info.si_signo = event.sigev_signo;
659 new_timer->sigq->info.si_value = event.sigev_value;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700660 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700661 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700662
Andrey Vagin2b08de02010-07-20 15:23:14 -0700663 if (copy_to_user(created_timer_id,
664 &new_timer_id, sizeof (new_timer_id))) {
665 error = -EFAULT;
666 goto out;
667 }
668
Thomas Gleixner838394f2011-02-01 13:51:58 +0000669 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700670 if (error)
671 goto out;
672
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700673 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800674 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700675 list_add(&new_timer->list, &current->signal->posix_timers);
676 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700677
678 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000679 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * In the case of the timer belonging to another task, after
681 * the task is unlocked, the timer is owned by the other task
682 * and may cease to exist at any time. Don't use or modify
683 * new_timer after the unlock call.
684 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700686 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return error;
688}
689
690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * Locking issues: We need to protect the result of the id look up until
692 * we get the timer locked down so it is not deleted under us. The
693 * removal is done under the idr spinlock so we use that here to bridge
694 * the find to the timer lock. To avoid a dead lock, the timer id MUST
695 * be release with out holding the timer lock.
696 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700697static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200700
Tejun Heoe182bb32013-02-20 15:24:12 -0800701 /*
702 * timer_t could be any type >= int and we want to make sure any
703 * @timer_id outside positive int range fails lookup.
704 */
705 if ((unsigned long long)timer_id > INT_MAX)
706 return NULL;
707
Eric Dumazet8af08872011-05-24 11:12:58 +0200708 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400709 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200711 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800712 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200713 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700714 return timr;
715 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200716 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700717 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200718 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Oleg Nesterov31d92842008-09-22 14:42:51 -0700720 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
724 * Get the time remaining on a POSIX.1b interval timer. This function
725 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
726 * mess with irq.
727 *
728 * We have a couple of messes to clean up here. First there is the case
729 * of a timer that has a requeue pending. These timers should appear to
730 * be in the timer list with an expiry as if we were to requeue them
731 * now.
732 *
733 * The second issue is the SIGEV_NONE timer which may be active but is
734 * not really ever put in the timer list (to save system resources).
735 * This timer may be expired, and if so, we will do it here. Otherwise
736 * it is the same as a requeue pending timer WRT to what we should
737 * report.
738 */
739static void
740common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
741{
Roman Zippel3b98a532006-03-26 01:38:07 -0800742 ktime_t now, remaining, iv;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800743 struct hrtimer *timer = &timr->it.real.timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800745 memset(cur_setting, 0, sizeof(struct itimerspec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Roman Zippel3b98a532006-03-26 01:38:07 -0800747 iv = timr->it.real.interval;
748
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800749 /* interval timer ? */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100750 if (iv)
Roman Zippel3b98a532006-03-26 01:38:07 -0800751 cur_setting->it_interval = ktime_to_timespec(iv);
752 else if (!hrtimer_active(timer) &&
753 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800754 return;
Roman Zippel3b98a532006-03-26 01:38:07 -0800755
756 now = timer->base->get_time();
757
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800758 /*
Roman Zippel3b98a532006-03-26 01:38:07 -0800759 * When a requeue is pending or this is a SIGEV_NONE
760 * timer move the expiry time forward by intervals, so
761 * expiry is > now.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800762 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100763 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
764 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800765 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
Roman Zippel3b98a532006-03-26 01:38:07 -0800766
Thomas Gleixner572c3912016-01-14 16:54:47 +0000767 remaining = __hrtimer_expires_remaining_adjusted(timer, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800768 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100769 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800770 /*
771 * A single shot SIGEV_NONE timer must return 0, when
772 * it is expired !
773 */
774 if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
775 cur_setting->it_value.tv_nsec = 1;
776 } else
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800777 cur_setting->it_value = ktime_to_timespec(remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/* Get the time remaining on a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100781SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
782 struct itimerspec __user *, setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct itimerspec cur_setting;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000785 struct k_itimer *timr;
786 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000788 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 timr = lock_timer(timer_id, &flags);
791 if (!timr)
792 return -EINVAL;
793
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000794 kc = clockid_to_kclock(timr->it_clock);
795 if (WARN_ON_ONCE(!kc || !kc->timer_get))
796 ret = -EINVAL;
797 else
798 kc->timer_get(timr, &cur_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 unlock_timer(timr, flags);
801
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000802 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EFAULT;
804
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000805 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * Get the number of overruns of a POSIX.1b interval timer. This is to
810 * be the overrun of the timer last delivered. At the same time we are
811 * accumulating overruns on the next timer. The overrun is frozen when
812 * the signal is delivered, either at the notify time (if the info block
813 * is not queued) or at the actual delivery time (as we are informed by
814 * the call back to do_schedule_next_timer(). So all we need to do is
815 * to pick up the frozen overrun.
816 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100817SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct k_itimer *timr;
820 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100821 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 timr = lock_timer(timer_id, &flags);
824 if (!timr)
825 return -EINVAL;
826
827 overrun = timr->it_overrun_last;
828 unlock_timer(timr, flags);
829
830 return overrun;
831}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833/* Set a POSIX.1b interval timer. */
834/* timr->it_lock is taken. */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800835static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836common_timer_set(struct k_itimer *timr, int flags,
837 struct itimerspec *new_setting, struct itimerspec *old_setting)
838{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800839 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800840 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (old_setting)
843 common_timer_get(timr, old_setting);
844
845 /* disable the timer */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100846 timr->it.real.interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * careful here. If smp we could be in the "fire" routine which will
849 * be spinning as we hold the lock. But this is ONLY an SMP issue.
850 */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800851 if (hrtimer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
855 ~REQUEUE_PENDING;
856 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800857
858 /* switch off the timer when it_value is zero */
859 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800862 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
George Anzinger7978672c2006-02-01 03:05:11 -0800863 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
George Anzinger7978672c2006-02-01 03:05:11 -0800864 timr->it.real.timer.function = posix_timer_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Arjan van de Vencc584b22008-09-01 15:02:30 -0700866 hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800867
868 /* Convert interval */
869 timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
870
871 /* SIGEV_NONE timers are not queued ! See common_timer_get */
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800872 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
873 /* Setup correct expiry time for relative timers */
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100874 if (mode == HRTIMER_MODE_REL) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700875 hrtimer_add_expires(timer, timer->base->get_time());
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100876 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800877 return 0;
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800878 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800879
Arjan van de Vencc584b22008-09-01 15:02:30 -0700880 hrtimer_start_expires(timer, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882}
883
884/* Set a POSIX.1b interval timer */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100885SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
886 const struct itimerspec __user *, new_setting,
887 struct itimerspec __user *, old_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct k_itimer *timr;
890 struct itimerspec new_spec, old_spec;
891 int error = 0;
Al Viro5ba25332007-10-14 19:35:50 +0100892 unsigned long flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000894 struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!new_setting)
897 return -EINVAL;
898
899 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
900 return -EFAULT;
901
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800902 if (!timespec_valid(&new_spec.it_interval) ||
903 !timespec_valid(&new_spec.it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -EINVAL;
905retry:
906 timr = lock_timer(timer_id, &flag);
907 if (!timr)
908 return -EINVAL;
909
Thomas Gleixner27722df2011-02-01 13:52:01 +0000910 kc = clockid_to_kclock(timr->it_clock);
911 if (WARN_ON_ONCE(!kc || !kc->timer_set))
912 error = -EINVAL;
913 else
914 error = kc->timer_set(timr, flags, &new_spec, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 unlock_timer(timr, flag);
917 if (error == TIMER_RETRY) {
918 rtn = NULL; // We already got the old time...
919 goto retry;
920 }
921
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800922 if (old_setting && !error &&
923 copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 error = -EFAULT;
925
926 return error;
927}
928
Thomas Gleixner6761c672011-02-01 13:52:07 +0000929static int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100931 timer->it.real.interval = 0;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700932
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800933 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
937
938static inline int timer_delete_hook(struct k_itimer *timer)
939{
Thomas Gleixner6761c672011-02-01 13:52:07 +0000940 struct k_clock *kc = clockid_to_kclock(timer->it_clock);
941
942 if (WARN_ON_ONCE(!kc || !kc->timer_del))
943 return -EINVAL;
944 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100948SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100951 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 timer = lock_timer(timer_id, &flags);
955 if (!timer)
956 return -EINVAL;
957
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800958 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unlock_timer(timer, flags);
960 goto retry_delete;
961 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 spin_lock(&current->sighand->siglock);
964 list_del(&timer->list);
965 spin_unlock(&current->sighand->siglock);
966 /*
967 * This keeps any tasks waiting on the spin lock from thinking
968 * they got something (see the lock code above).
969 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800970 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unlock_timer(timer, flags);
973 release_posix_timer(timer, IT_ID_SET);
974 return 0;
975}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
978 * return timer owned by the process, used by exit_itimers
979 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800980static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 unsigned long flags;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_lock_irqsave(&timer->it_lock, flags);
986
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800987 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unlock_timer(timer, flags);
989 goto retry_delete;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 list_del(&timer->list);
992 /*
993 * This keeps any tasks waiting on the spin lock from thinking
994 * they got something (see the lock code above).
995 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800996 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unlock_timer(timer, flags);
999 release_posix_timer(timer, IT_ID_SET);
1000}
1001
1002/*
Roland McGrath25f407f2005-10-21 15:03:29 -07001003 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * references to the shared signal_struct.
1005 */
1006void exit_itimers(struct signal_struct *sig)
1007{
1008 struct k_itimer *tmr;
1009
1010 while (!list_empty(&sig->posix_timers)) {
1011 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1012 itimer_delete(tmr);
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Heiko Carstens362e9c02009-01-14 14:14:07 +01001016SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1017 const struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001019 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct timespec new_tp;
1021
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001022 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1026 return -EFAULT;
1027
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001028 return kc->clock_set(which_clock, &new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Heiko Carstens362e9c02009-01-14 14:14:07 +01001031SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1032 struct timespec __user *,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Thomas Gleixner42285772011-02-01 13:51:50 +00001034 struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001035 struct timespec64 kernel_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct timespec kernel_tp;
1037 int error;
1038
Thomas Gleixner42285772011-02-01 13:51:50 +00001039 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001041
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001042 error = kc->clock_get(which_clock, &kernel_tp64);
1043 kernel_tp = timespec64_to_timespec(kernel_tp64);
Thomas Gleixner42285772011-02-01 13:51:50 +00001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1046 error = -EFAULT;
1047
1048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001051SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1052 struct timex __user *, utx)
1053{
1054 struct k_clock *kc = clockid_to_kclock(which_clock);
1055 struct timex ktx;
1056 int err;
1057
1058 if (!kc)
1059 return -EINVAL;
1060 if (!kc->clock_adj)
1061 return -EOPNOTSUPP;
1062
1063 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1064 return -EFAULT;
1065
1066 err = kc->clock_adj(which_clock, &ktx);
1067
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001068 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001069 return -EFAULT;
1070
1071 return err;
1072}
1073
Heiko Carstens362e9c02009-01-14 14:14:07 +01001074SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1075 struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001077 struct k_clock *kc = clockid_to_kclock(which_clock);
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
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001084 error = kc->clock_getres(which_clock, &rtn_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001086 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 return error;
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001093 * nanosleep for monotonic and realtime clocks
1094 */
1095static int common_nsleep(const clockid_t which_clock, int flags,
1096 struct timespec *tsave, struct timespec __user *rmtp)
1097{
Oleg Nesterov080344b2008-02-01 17:29:05 +03001098 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
1099 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1100 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Heiko Carstens362e9c02009-01-14 14:14:07 +01001103SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1104 const struct timespec __user *, rqtp,
1105 struct timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001107 struct k_clock *kc = clockid_to_kclock(which_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001110 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001112 if (!kc->nsleep)
1113 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1116 return -EFAULT;
1117
Thomas Gleixner5f82b2b2006-01-09 20:52:29 -08001118 if (!timespec_valid(&t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EINVAL;
1120
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001121 return kc->nsleep(which_clock, flags, &t, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
Toyo Abe1711ef32006-09-29 02:00:28 -07001123
1124/*
Toyo Abe1711ef32006-09-29 02:00:28 -07001125 * This will restart clock_nanosleep. This is required only by
1126 * compat_clock_nanosleep_restart for now.
1127 */
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001128long clock_nanosleep_restart(struct restart_block *restart_block)
Toyo Abe1711ef32006-09-29 02:00:28 -07001129{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001130 clockid_t which_clock = restart_block->nanosleep.clockid;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001131 struct k_clock *kc = clockid_to_kclock(which_clock);
Toyo Abe1711ef32006-09-29 02:00:28 -07001132
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001133 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
1134 return -EINVAL;
1135
1136 return kc->nsleep_restart(restart_block);
Toyo Abe1711ef32006-09-29 02:00:28 -07001137}