blob: b60b655dfbcdd8ef2291c77c1b683fcf64c2d584 [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
Thomas Gleixner6631fa12017-05-30 23:15:39 +020072static const struct k_clock * const posix_clocks[];
73static const struct k_clock *clockid_to_kclock(const clockid_t id);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
77 * SIGEV values. Here we put out an error if this assumption fails.
78 */
79#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
80 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
81#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
82#endif
83
Thomas Gleixner65da5282011-02-01 13:51:01 +000084/*
85 * parisc wants ENOTSUP instead of EOPNOTSUPP
86 */
87#ifndef ENOTSUP
88# define ENANOSLEEP_NOTSUP EOPNOTSUPP
89#else
90# define ENANOSLEEP_NOTSUP ENOTSUP
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * The timer ID is turned into a timer address by idr_find().
95 * Verifying a valid ID consists of:
96 *
97 * a) checking that idr_find() returns other than -1.
98 * b) checking that the timer id matches the one in the timer itself.
99 * c) that the timer owner is in the callers thread group.
100 */
101
102/*
103 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
104 * to implement others. This structure defines the various
Richard Cochran00617482011-02-01 13:52:15 +0000105 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * RESOLUTION: Clock resolution is used to round up timer and interval
108 * times, NOT to report clock times, which are reported with as
109 * much resolution as the system can muster. In some cases this
110 * resolution may depend on the underlying clock hardware and
111 * may not be quantifiable until run time, and only then is the
112 * necessary code is written. The standard says we should say
113 * something about this issue in the documentation...
114 *
Richard Cochran00617482011-02-01 13:52:15 +0000115 * FUNCTIONS: The CLOCKs structure defines possible functions to
116 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
Richard Cochran00617482011-02-01 13:52:15 +0000118 * The standard POSIX timer management code assumes the
119 * following: 1.) The k_itimer struct (sched.h) is used for
120 * the timer. 2.) The list, it_lock, it_clock, it_id and
121 * it_pid fields are not modified by timer code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
123 * Permissions: It is assumed that the clock_settime() function defined
124 * for each clock will take care of permission checks. Some
125 * clocks may be set able by any user (i.e. local process
126 * clocks) others not. Currently the only set able clock we
127 * have is CLOCK_REALTIME and its high res counter part, both of
128 * which we beg off on and pass to do_sys_settimeofday().
129 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700130static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
131
132#define lock_timer(tid, flags) \
133({ struct k_itimer *__timr; \
134 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
135 __timr; \
136})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400138static int hash(struct signal_struct *sig, unsigned int nr)
139{
140 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
141}
142
143static struct k_itimer *__posix_timers_find(struct hlist_head *head,
144 struct signal_struct *sig,
145 timer_t id)
146{
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400147 struct k_itimer *timer;
148
149 hlist_for_each_entry_rcu(timer, head, t_hash) {
150 if ((timer->it_signal == sig) && (timer->it_id == id))
151 return timer;
152 }
153 return NULL;
154}
155
156static struct k_itimer *posix_timer_by_id(timer_t id)
157{
158 struct signal_struct *sig = current->signal;
159 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
160
161 return __posix_timers_find(head, sig, id);
162}
163
164static int posix_timer_add(struct k_itimer *timer)
165{
166 struct signal_struct *sig = current->signal;
167 int first_free_id = sig->posix_timer_id;
168 struct hlist_head *head;
169 int ret = -ENOENT;
170
171 do {
172 spin_lock(&hash_lock);
173 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
174 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
175 hlist_add_head_rcu(&timer->t_hash, head);
176 ret = sig->posix_timer_id;
177 }
178 if (++sig->posix_timer_id < 0)
179 sig->posix_timer_id = 0;
180 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
181 /* Loop over all possible ids completed */
182 ret = -EAGAIN;
183 spin_unlock(&hash_lock);
184 } while (ret == -ENOENT);
185 return ret;
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
189{
190 spin_unlock_irqrestore(&timr->it_lock, flags);
191}
192
Thomas Gleixner42285772011-02-01 13:51:50 +0000193/* Get clock_realtime */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700194static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner42285772011-02-01 13:51:50 +0000195{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700196 ktime_get_real_ts64(tp);
Thomas Gleixner42285772011-02-01 13:51:50 +0000197 return 0;
198}
199
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000200/* Set clock_realtime */
201static int posix_clock_realtime_set(const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700202 const struct timespec64 *tp)
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000203{
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700204 return do_sys_settimeofday64(tp, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000205}
206
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000207static int posix_clock_realtime_adj(const clockid_t which_clock,
208 struct timex *t)
209{
210 return do_adjtimex(t);
211}
212
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800213/*
214 * Get monotonic time for posix timers
215 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700216static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800217{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700218 ktime_get_ts64(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800219 return 0;
220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800223 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700224 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700225static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700226{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700227 getrawmonotonic64(tp);
John Stultz2d422442008-08-20 16:37:30 -0700228 return 0;
229}
230
john stultzda15cfd2009-08-19 19:13:34 -0700231
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700232static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700233{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700234 *tp = current_kernel_time64();
john stultzda15cfd2009-08-19 19:13:34 -0700235 return 0;
236}
237
238static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700239 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700240{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700241 *tp = get_monotonic_coarse64();
john stultzda15cfd2009-08-19 19:13:34 -0700242 return 0;
243}
244
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700245static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700246{
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700247 *tp = ktime_to_timespec64(KTIME_LOW_RES);
john stultzda15cfd2009-08-19 19:13:34 -0700248 return 0;
249}
John Stultz7fdd7f82011-02-15 10:52:57 -0800250
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700251static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
John Stultz7fdd7f82011-02-15 10:52:57 -0800252{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700253 get_monotonic_boottime64(tp);
John Stultz7fdd7f82011-02-15 10:52:57 -0800254 return 0;
255}
256
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700257static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700258{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700259 timekeeping_clocktai64(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700260 return 0;
261}
John Stultz7fdd7f82011-02-15 10:52:57 -0800262
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700263static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000264{
265 tp->tv_sec = 0;
266 tp->tv_nsec = hrtimer_resolution;
267 return 0;
268}
269
John Stultz2d422442008-08-20 16:37:30 -0700270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * Initialize everything, well, just everything in Posix clocks/timers ;)
272 */
273static __init int init_posix_timers(void)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700276 sizeof (struct k_itimer), 0, SLAB_PANIC,
277 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
280
281__initcall(init_posix_timers);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static void schedule_next_timer(struct k_itimer *timr)
284{
Roman Zippel44f21472006-03-26 01:38:06 -0800285 struct hrtimer *timer = &timr->it.real.timer;
286
Thomas Gleixner2456e852016-12-25 11:38:40 +0100287 if (timr->it.real.interval == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return;
289
Davide Libenzi4d672e72008-02-04 22:27:26 -0800290 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
291 timer->base->get_time(),
292 timr->it.real.interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 timr->it_overrun_last = timr->it_overrun;
295 timr->it_overrun = -1;
296 ++timr->it_requeue_pending;
Roman Zippel44f21472006-03-26 01:38:06 -0800297 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
301 * This function is exported for use by the signal deliver code. It is
302 * called just prior to the info block being released and passes that
303 * block to us. It's function is to update the overrun entry AND to
304 * restart the timer. It should only be called if the timer is to be
305 * restarted (i.e. we have flagged this in the sys_private entry of the
306 * info block).
307 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300308 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * we require that the it_requeue_pending flag be set.
310 */
311void do_schedule_next_timer(struct siginfo *info)
312{
313 struct k_itimer *timr;
314 unsigned long flags;
315
316 timr = lock_timer(info->si_tid, &flags);
317
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800318 if (timr && timr->it_requeue_pending == info->si_sys_private) {
319 if (timr->it_clock < 0)
320 posix_cpu_timer_schedule(timr);
321 else
322 schedule_next_timer(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Oleg Nesterov54da1172008-07-23 20:52:05 +0400324 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800325 }
326
Thomas Gleixnerb6557fb2006-02-01 03:05:09 -0800327 if (timr)
328 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Oleg Nesterovba661292008-07-23 20:52:05 +0400331int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800333 struct task_struct *task;
334 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400335 /*
336 * FIXME: if ->sigq is queued we can race with
337 * dequeue_signal()->do_schedule_next_timer().
338 *
339 * If dequeue_signal() sees the "right" value of
340 * si_sys_private it calls do_schedule_next_timer().
341 * We re-queue ->sigq and drop ->it_lock().
342 * do_schedule_next_timer() locks the timer
343 * and re-schedules it while ->sigq is pending.
344 * Not really bad, but not that we want.
345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Oleg Nesterov27af4242008-12-01 14:18:13 -0800348 rcu_read_lock();
349 task = pid_task(timr->it_pid, PIDTYPE_PID);
350 if (task) {
351 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
352 ret = send_sigqueue(timr->sigq, task, shared);
353 }
354 rcu_read_unlock();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700355 /* If we failed to send the signal the timer stops. */
356 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*
360 * This function gets called when a POSIX.1b interval timer expires. It
361 * is used as a callback from the kernel internal timer. The
362 * run_timer_list code ALWAYS calls with interrupts on.
363
364 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
365 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800366static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Roman Zippel05cfb612006-03-26 01:38:12 -0800368 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800370 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800371 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Roman Zippel05cfb612006-03-26 01:38:12 -0800373 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800375
Thomas Gleixner2456e852016-12-25 11:38:40 +0100376 if (timr->it.real.interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800377 si_private = ++timr->it_requeue_pending;
378
379 if (posix_timer_event(timr, si_private)) {
380 /*
381 * signal was not sent because of sig_ignor
382 * we will not get a call back to restart it AND
383 * it should be restarted.
384 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100385 if (timr->it.real.interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000386 ktime_t now = hrtimer_cb_get_time(timer);
387
388 /*
389 * FIXME: What we really want, is to stop this
390 * timer completely and restart it in case the
391 * SIG_IGN is removed. This is a non trivial
392 * change which involves sighand locking
393 * (sigh !), which we don't want to do late in
394 * the release cycle.
395 *
396 * For now we just let timers with an interval
397 * less than a jiffie expire every jiffie to
398 * avoid softirq starvation in case of SIG_IGN
399 * and a very small interval, which would put
400 * the timer right back on the softirq pending
401 * list. By moving now ahead of time we trick
402 * hrtimer_forward() to expire the timer
403 * later, while we still maintain the overrun
404 * accuracy, but have some inconsistency in
405 * the timer_gettime() case. This is at least
406 * better than a starved softirq. A more
407 * complex fix which solves also another related
408 * inconsistency is already in the pipeline.
409 */
410#ifdef CONFIG_HIGH_RES_TIMERS
411 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100412 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000413
Thomas Gleixner2456e852016-12-25 11:38:40 +0100414 if (timr->it.real.interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000415 now = ktime_add(now, kj);
416 }
417#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800418 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000419 hrtimer_forward(timer, now,
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800420 timr->it.real.interval);
421 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800422 ++timr->it_requeue_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800426 unlock_timer(timr, flags);
427 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Oleg Nesterov27af4242008-12-01 14:18:13 -0800430static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct task_struct *rtn = current->group_leader;
433
434 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800435 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700436 !same_thread_group(rtn, current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
438 return NULL;
439
440 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
441 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
442 return NULL;
443
Oleg Nesterov27af4242008-12-01 14:18:13 -0800444 return task_pid(rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static struct k_itimer * alloc_posix_timer(void)
448{
449 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800450 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!tmr)
452 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
454 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700455 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Oleg Nesterovba661292008-07-23 20:52:05 +0400457 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return tmr;
459}
460
Eric Dumazet8af08872011-05-24 11:12:58 +0200461static void k_itimer_rcu_free(struct rcu_head *head)
462{
463 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
464
465 kmem_cache_free(posix_timers_cache, tmr);
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#define IT_ID_SET 1
469#define IT_ID_NOT_SET 0
470static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
471{
472 if (it_id_set) {
473 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400474 spin_lock_irqsave(&hash_lock, flags);
475 hlist_del_rcu(&tmr->t_hash);
476 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800478 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200480 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Thomas Gleixner838394f2011-02-01 13:51:58 +0000483static int common_timer_create(struct k_itimer *new_timer)
484{
485 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
486 return 0;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/* Create a POSIX.1b interval timer. */
490
Heiko Carstens362e9c02009-01-14 14:14:07 +0100491SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
492 struct sigevent __user *, timer_event_spec,
493 timer_t __user *, created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300495 const struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700496 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700497 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 sigevent_t event;
499 int it_id_set = IT_ID_NOT_SET;
500
Thomas Gleixner838394f2011-02-01 13:51:58 +0000501 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000503 if (!kc->timer_create)
504 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 new_timer = alloc_posix_timer();
507 if (unlikely(!new_timer))
508 return -EAGAIN;
509
510 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400511 new_timer_id = posix_timer_add(new_timer);
512 if (new_timer_id < 0) {
513 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out;
515 }
516
517 it_id_set = IT_ID_SET;
518 new_timer->it_id = (timer_t) new_timer_id;
519 new_timer->it_clock = which_clock;
520 new_timer->it_overrun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (timer_event_spec) {
523 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
524 error = -EFAULT;
525 goto out;
526 }
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700527 rcu_read_lock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800528 new_timer->it_pid = get_pid(good_sigevent(&event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700529 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800530 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 error = -EINVAL;
532 goto out;
533 }
534 } else {
Mathias Krause6891c452014-10-04 23:06:39 +0200535 memset(&event.sigev_value, 0, sizeof(event.sigev_value));
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700536 event.sigev_notify = SIGEV_SIGNAL;
537 event.sigev_signo = SIGALRM;
538 event.sigev_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800539 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700542 new_timer->it_sigev_notify = event.sigev_notify;
543 new_timer->sigq->info.si_signo = event.sigev_signo;
544 new_timer->sigq->info.si_value = event.sigev_value;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700545 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700546 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700547
Andrey Vagin2b08de02010-07-20 15:23:14 -0700548 if (copy_to_user(created_timer_id,
549 &new_timer_id, sizeof (new_timer_id))) {
550 error = -EFAULT;
551 goto out;
552 }
553
Thomas Gleixner838394f2011-02-01 13:51:58 +0000554 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700555 if (error)
556 goto out;
557
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700558 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800559 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700560 list_add(&new_timer->list, &current->signal->posix_timers);
561 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700562
563 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000564 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * In the case of the timer belonging to another task, after
566 * the task is unlocked, the timer is owned by the other task
567 * and may cease to exist at any time. Don't use or modify
568 * new_timer after the unlock call.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700571 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return error;
573}
574
575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * Locking issues: We need to protect the result of the id look up until
577 * we get the timer locked down so it is not deleted under us. The
578 * removal is done under the idr spinlock so we use that here to bridge
579 * the find to the timer lock. To avoid a dead lock, the timer id MUST
580 * be release with out holding the timer lock.
581 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700582static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200585
Tejun Heoe182bb32013-02-20 15:24:12 -0800586 /*
587 * timer_t could be any type >= int and we want to make sure any
588 * @timer_id outside positive int range fails lookup.
589 */
590 if ((unsigned long long)timer_id > INT_MAX)
591 return NULL;
592
Eric Dumazet8af08872011-05-24 11:12:58 +0200593 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400594 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200596 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800597 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200598 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700599 return timr;
600 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200601 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700602 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200603 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Oleg Nesterov31d92842008-09-22 14:42:51 -0700605 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
609 * Get the time remaining on a POSIX.1b interval timer. This function
610 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
611 * mess with irq.
612 *
613 * We have a couple of messes to clean up here. First there is the case
614 * of a timer that has a requeue pending. These timers should appear to
615 * be in the timer list with an expiry as if we were to requeue them
616 * now.
617 *
618 * The second issue is the SIGEV_NONE timer which may be active but is
619 * not really ever put in the timer list (to save system resources).
620 * This timer may be expired, and if so, we will do it here. Otherwise
621 * it is the same as a requeue pending timer WRT to what we should
622 * report.
623 */
624static void
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700625common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Roman Zippel3b98a532006-03-26 01:38:07 -0800627 ktime_t now, remaining, iv;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800628 struct hrtimer *timer = &timr->it.real.timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700630 memset(cur_setting, 0, sizeof(*cur_setting));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Roman Zippel3b98a532006-03-26 01:38:07 -0800632 iv = timr->it.real.interval;
633
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800634 /* interval timer ? */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100635 if (iv)
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700636 cur_setting->it_interval = ktime_to_timespec64(iv);
Roman Zippel3b98a532006-03-26 01:38:07 -0800637 else if (!hrtimer_active(timer) &&
638 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800639 return;
Roman Zippel3b98a532006-03-26 01:38:07 -0800640
641 now = timer->base->get_time();
642
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800643 /*
Roman Zippel3b98a532006-03-26 01:38:07 -0800644 * When a requeue is pending or this is a SIGEV_NONE
645 * timer move the expiry time forward by intervals, so
646 * expiry is > now.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800647 */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100648 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
649 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
Davide Libenzi4d672e72008-02-04 22:27:26 -0800650 timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
Roman Zippel3b98a532006-03-26 01:38:07 -0800651
Thomas Gleixner572c3912016-01-14 16:54:47 +0000652 remaining = __hrtimer_expires_remaining_adjusted(timer, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800653 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100654 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800655 /*
656 * A single shot SIGEV_NONE timer must return 0, when
657 * it is expired !
658 */
659 if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
660 cur_setting->it_value.tv_nsec = 1;
661 } else
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700662 cur_setting->it_value = ktime_to_timespec64(remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/* Get the time remaining on a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100666SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
667 struct itimerspec __user *, setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700669 struct itimerspec64 cur_setting64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct itimerspec cur_setting;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000671 struct k_itimer *timr;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300672 const struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000674 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 timr = lock_timer(timer_id, &flags);
677 if (!timr)
678 return -EINVAL;
679
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000680 kc = clockid_to_kclock(timr->it_clock);
681 if (WARN_ON_ONCE(!kc || !kc->timer_get))
682 ret = -EINVAL;
683 else
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700684 kc->timer_get(timr, &cur_setting64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 unlock_timer(timr, flags);
687
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700688 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000689 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EFAULT;
691
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Get the number of overruns of a POSIX.1b interval timer. This is to
697 * be the overrun of the timer last delivered. At the same time we are
698 * accumulating overruns on the next timer. The overrun is frozen when
699 * the signal is delivered, either at the notify time (if the info block
700 * is not queued) or at the actual delivery time (as we are informed by
701 * the call back to do_schedule_next_timer(). So all we need to do is
702 * to pick up the frozen overrun.
703 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100704SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct k_itimer *timr;
707 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100708 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 timr = lock_timer(timer_id, &flags);
711 if (!timr)
712 return -EINVAL;
713
714 overrun = timr->it_overrun_last;
715 unlock_timer(timr, flags);
716
717 return overrun;
718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720/* Set a POSIX.1b interval timer. */
721/* timr->it_lock is taken. */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800722static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723common_timer_set(struct k_itimer *timr, int flags,
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700724 struct itimerspec64 *new_setting, struct itimerspec64 *old_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800726 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800727 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (old_setting)
730 common_timer_get(timr, old_setting);
731
732 /* disable the timer */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100733 timr->it.real.interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * careful here. If smp we could be in the "fire" routine which will
736 * be spinning as we hold the lock. But this is ONLY an SMP issue.
737 */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800738 if (hrtimer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
742 ~REQUEUE_PENDING;
743 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800744
745 /* switch off the timer when it_value is zero */
746 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800749 mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
George Anzinger7978672c2006-02-01 03:05:11 -0800750 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
George Anzinger7978672c2006-02-01 03:05:11 -0800751 timr->it.real.timer.function = posix_timer_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700753 hrtimer_set_expires(timer, timespec64_to_ktime(new_setting->it_value));
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800754
755 /* Convert interval */
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700756 timr->it.real.interval = timespec64_to_ktime(new_setting->it_interval);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800757
758 /* SIGEV_NONE timers are not queued ! See common_timer_get */
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800759 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
760 /* Setup correct expiry time for relative timers */
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100761 if (mode == HRTIMER_MODE_REL) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700762 hrtimer_add_expires(timer, timer->base->get_time());
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100763 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800764 return 0;
Thomas Gleixner952bbc82006-02-01 03:05:13 -0800765 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800766
Arjan van de Vencc584b22008-09-01 15:02:30 -0700767 hrtimer_start_expires(timer, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769}
770
771/* Set a POSIX.1b interval timer */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100772SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
773 const struct itimerspec __user *, new_setting,
774 struct itimerspec __user *, old_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700776 struct itimerspec64 new_spec64, old_spec64;
777 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct itimerspec new_spec, old_spec;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700779 struct k_itimer *timr;
Al Viro5ba25332007-10-14 19:35:50 +0100780 unsigned long flag;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300781 const struct k_clock *kc;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700782 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (!new_setting)
785 return -EINVAL;
786
787 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
788 return -EFAULT;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700789 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700791 if (!timespec64_valid(&new_spec64.it_interval) ||
792 !timespec64_valid(&new_spec64.it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -EINVAL;
794retry:
795 timr = lock_timer(timer_id, &flag);
796 if (!timr)
797 return -EINVAL;
798
Thomas Gleixner27722df2011-02-01 13:52:01 +0000799 kc = clockid_to_kclock(timr->it_clock);
800 if (WARN_ON_ONCE(!kc || !kc->timer_set))
801 error = -EINVAL;
802 else
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700803 error = kc->timer_set(timr, flags, &new_spec64, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 unlock_timer(timr, flag);
806 if (error == TIMER_RETRY) {
807 rtn = NULL; // We already got the old time...
808 goto retry;
809 }
810
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700811 old_spec = itimerspec64_to_itimerspec(&old_spec64);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800812 if (old_setting && !error &&
813 copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 error = -EFAULT;
815
816 return error;
817}
818
Thomas Gleixner6761c672011-02-01 13:52:07 +0000819static int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Thomas Gleixner2456e852016-12-25 11:38:40 +0100821 timer->it.real.interval = 0;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700822
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800823 if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
828static inline int timer_delete_hook(struct k_itimer *timer)
829{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300830 const struct k_clock *kc = clockid_to_kclock(timer->it_clock);
Thomas Gleixner6761c672011-02-01 13:52:07 +0000831
832 if (WARN_ON_ONCE(!kc || !kc->timer_del))
833 return -EINVAL;
834 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100838SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 timer = lock_timer(timer_id, &flags);
845 if (!timer)
846 return -EINVAL;
847
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800848 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unlock_timer(timer, flags);
850 goto retry_delete;
851 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_lock(&current->sighand->siglock);
854 list_del(&timer->list);
855 spin_unlock(&current->sighand->siglock);
856 /*
857 * This keeps any tasks waiting on the spin lock from thinking
858 * they got something (see the lock code above).
859 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800860 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unlock_timer(timer, flags);
863 release_posix_timer(timer, IT_ID_SET);
864 return 0;
865}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * return timer owned by the process, used by exit_itimers
869 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800870static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 unsigned long flags;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 spin_lock_irqsave(&timer->it_lock, flags);
876
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800877 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unlock_timer(timer, flags);
879 goto retry_delete;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 list_del(&timer->list);
882 /*
883 * This keeps any tasks waiting on the spin lock from thinking
884 * they got something (see the lock code above).
885 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800886 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unlock_timer(timer, flags);
889 release_posix_timer(timer, IT_ID_SET);
890}
891
892/*
Roland McGrath25f407f2005-10-21 15:03:29 -0700893 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * references to the shared signal_struct.
895 */
896void exit_itimers(struct signal_struct *sig)
897{
898 struct k_itimer *tmr;
899
900 while (!list_empty(&sig->posix_timers)) {
901 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
902 itimer_delete(tmr);
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Heiko Carstens362e9c02009-01-14 14:14:07 +0100906SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
907 const struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300909 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700910 struct timespec64 new_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct timespec new_tp;
912
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000913 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
917 return -EFAULT;
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700918 new_tp64 = timespec_to_timespec64(new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700920 return kc->clock_set(which_clock, &new_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Heiko Carstens362e9c02009-01-14 14:14:07 +0100923SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
924 struct timespec __user *,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300926 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700927 struct timespec64 kernel_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct timespec kernel_tp;
929 int error;
930
Thomas Gleixner42285772011-02-01 13:51:50 +0000931 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +0000933
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700934 error = kc->clock_get(which_clock, &kernel_tp64);
935 kernel_tp = timespec64_to_timespec(kernel_tp64);
Thomas Gleixner42285772011-02-01 13:51:50 +0000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
938 error = -EFAULT;
939
940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000943SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
944 struct timex __user *, utx)
945{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300946 const struct k_clock *kc = clockid_to_kclock(which_clock);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000947 struct timex ktx;
948 int err;
949
950 if (!kc)
951 return -EINVAL;
952 if (!kc->clock_adj)
953 return -EOPNOTSUPP;
954
955 if (copy_from_user(&ktx, utx, sizeof(ktx)))
956 return -EFAULT;
957
958 err = kc->clock_adj(which_clock, &ktx);
959
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +0100960 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000961 return -EFAULT;
962
963 return err;
964}
965
Heiko Carstens362e9c02009-01-14 14:14:07 +0100966SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
967 struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300969 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700970 struct timespec64 rtn_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct timespec rtn_tp;
972 int error;
973
Thomas Gleixnere5e542e2011-02-01 13:51:53 +0000974 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EINVAL;
976
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700977 error = kc->clock_getres(which_clock, &rtn_tp64);
978 rtn_tp = timespec64_to_timespec(rtn_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Thomas Gleixnere5e542e2011-02-01 13:51:53 +0000980 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 return error;
984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
Thomas Gleixner97735f22006-01-09 20:52:37 -0800987 * nanosleep for monotonic and realtime clocks
988 */
989static int common_nsleep(const clockid_t which_clock, int flags,
Deepa Dinamaniad196382017-03-26 12:04:18 -0700990 struct timespec64 *tsave, struct timespec __user *rmtp)
Thomas Gleixner97735f22006-01-09 20:52:37 -0800991{
Oleg Nesterov080344b2008-02-01 17:29:05 +0300992 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
993 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
994 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -0800995}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Heiko Carstens362e9c02009-01-14 14:14:07 +0100997SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
998 const struct timespec __user *, rqtp,
999 struct timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001001 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamaniad196382017-03-26 12:04:18 -07001002 struct timespec64 t64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001005 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001007 if (!kc->nsleep)
1008 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1011 return -EFAULT;
1012
Deepa Dinamaniad196382017-03-26 12:04:18 -07001013 t64 = timespec_to_timespec64(t);
1014 if (!timespec64_valid(&t64))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -EINVAL;
1016
Deepa Dinamaniad196382017-03-26 12:04:18 -07001017 return kc->nsleep(which_clock, flags, &t64, rmtp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Toyo Abe1711ef32006-09-29 02:00:28 -07001019
1020/*
Toyo Abe1711ef32006-09-29 02:00:28 -07001021 * This will restart clock_nanosleep. This is required only by
1022 * compat_clock_nanosleep_restart for now.
1023 */
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001024long clock_nanosleep_restart(struct restart_block *restart_block)
Toyo Abe1711ef32006-09-29 02:00:28 -07001025{
Thomas Gleixnerab8177b2011-05-20 13:05:15 +02001026 clockid_t which_clock = restart_block->nanosleep.clockid;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001027 const struct k_clock *kc = clockid_to_kclock(which_clock);
Toyo Abe1711ef32006-09-29 02:00:28 -07001028
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001029 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
1030 return -EINVAL;
1031
1032 return kc->nsleep_restart(restart_block);
Toyo Abe1711ef32006-09-29 02:00:28 -07001033}
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001034
1035static const struct k_clock clock_realtime = {
1036 .clock_getres = posix_get_hrtimer_res,
1037 .clock_get = posix_clock_realtime_get,
1038 .clock_set = posix_clock_realtime_set,
1039 .clock_adj = posix_clock_realtime_adj,
1040 .nsleep = common_nsleep,
1041 .nsleep_restart = hrtimer_nanosleep_restart,
1042 .timer_create = common_timer_create,
1043 .timer_set = common_timer_set,
1044 .timer_get = common_timer_get,
1045 .timer_del = common_timer_del,
1046};
1047
1048static const struct k_clock clock_monotonic = {
1049 .clock_getres = posix_get_hrtimer_res,
1050 .clock_get = posix_ktime_get_ts,
1051 .nsleep = common_nsleep,
1052 .nsleep_restart = hrtimer_nanosleep_restart,
1053 .timer_create = common_timer_create,
1054 .timer_set = common_timer_set,
1055 .timer_get = common_timer_get,
1056 .timer_del = common_timer_del,
1057};
1058
1059static const struct k_clock clock_monotonic_raw = {
1060 .clock_getres = posix_get_hrtimer_res,
1061 .clock_get = posix_get_monotonic_raw,
1062};
1063
1064static const struct k_clock clock_realtime_coarse = {
1065 .clock_getres = posix_get_coarse_res,
1066 .clock_get = posix_get_realtime_coarse,
1067};
1068
1069static const struct k_clock clock_monotonic_coarse = {
1070 .clock_getres = posix_get_coarse_res,
1071 .clock_get = posix_get_monotonic_coarse,
1072};
1073
1074static const struct k_clock clock_tai = {
1075 .clock_getres = posix_get_hrtimer_res,
1076 .clock_get = posix_get_tai,
1077 .nsleep = common_nsleep,
1078 .nsleep_restart = hrtimer_nanosleep_restart,
1079 .timer_create = common_timer_create,
1080 .timer_set = common_timer_set,
1081 .timer_get = common_timer_get,
1082 .timer_del = common_timer_del,
1083};
1084
1085static const struct k_clock clock_boottime = {
1086 .clock_getres = posix_get_hrtimer_res,
1087 .clock_get = posix_get_boottime,
1088 .nsleep = common_nsleep,
1089 .nsleep_restart = hrtimer_nanosleep_restart,
1090 .timer_create = common_timer_create,
1091 .timer_set = common_timer_set,
1092 .timer_get = common_timer_get,
1093 .timer_del = common_timer_del,
1094};
1095
1096static const struct k_clock * const posix_clocks[] = {
1097 [CLOCK_REALTIME] = &clock_realtime,
1098 [CLOCK_MONOTONIC] = &clock_monotonic,
1099 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1100 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1101 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1102 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1103 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1104 [CLOCK_BOOTTIME] = &clock_boottime,
1105 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1106 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1107 [CLOCK_TAI] = &clock_tai,
1108};
1109
1110static const struct k_clock *clockid_to_kclock(const clockid_t id)
1111{
1112 if (id < 0)
1113 return (id & CLOCKFD_MASK) == CLOCKFD ?
1114 &clock_posix_dynamic : &clock_posix_cpu;
1115
1116 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1117 return NULL;
1118 return posix_clocks[id];
1119}