blob: 2bdf08a2bae96ef662dce82defb3ff2e912e1c70 [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>
Al Viroedbeda42017-06-07 09:42:31 +010052#include <linux/compat.h>
Thomas Gleixner19b558d2018-02-15 17:21:55 +010053#include <linux/nospec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000055#include "timekeeping.h"
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020056#include "posix-timers.h"
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040059 * Management arrays for POSIX timers. Timers are now kept in static hash table
60 * with 512 entries.
61 * Timer ids are allocated by local routine, which selects proper hash head by
62 * key, constructed from current->signal address and per signal struct counter.
63 * This keeps timer ids unique per process, but now they can intersect between
64 * processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
67/*
68 * Lets keep our timers in a slab cache :-)
69 */
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *posix_timers_cache;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040071
72static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
73static DEFINE_SPINLOCK(hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Thomas Gleixner6631fa12017-05-30 23:15:39 +020075static const struct k_clock * const posix_clocks[];
76static const struct k_clock *clockid_to_kclock(const clockid_t id);
Thomas Gleixner67edab42017-06-12 19:39:49 +020077static const struct k_clock clock_realtime, clock_monotonic;
Thomas Gleixner6631fa12017-05-30 23:15:39 +020078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
81 * SIGEV values. Here we put out an error if this assumption fails.
82 */
83#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
84 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
85#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
86#endif
87
Thomas Gleixner65da5282011-02-01 13:51:01 +000088/*
89 * parisc wants ENOTSUP instead of EOPNOTSUPP
90 */
91#ifndef ENOTSUP
92# define ENANOSLEEP_NOTSUP EOPNOTSUPP
93#else
94# define ENANOSLEEP_NOTSUP ENOTSUP
95#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * The timer ID is turned into a timer address by idr_find().
99 * Verifying a valid ID consists of:
100 *
101 * a) checking that idr_find() returns other than -1.
102 * b) checking that the timer id matches the one in the timer itself.
103 * c) that the timer owner is in the callers thread group.
104 */
105
106/*
107 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
108 * to implement others. This structure defines the various
Richard Cochran00617482011-02-01 13:52:15 +0000109 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 * RESOLUTION: Clock resolution is used to round up timer and interval
112 * times, NOT to report clock times, which are reported with as
113 * much resolution as the system can muster. In some cases this
114 * resolution may depend on the underlying clock hardware and
115 * may not be quantifiable until run time, and only then is the
116 * necessary code is written. The standard says we should say
117 * something about this issue in the documentation...
118 *
Richard Cochran00617482011-02-01 13:52:15 +0000119 * FUNCTIONS: The CLOCKs structure defines possible functions to
120 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
Richard Cochran00617482011-02-01 13:52:15 +0000122 * The standard POSIX timer management code assumes the
123 * following: 1.) The k_itimer struct (sched.h) is used for
124 * the timer. 2.) The list, it_lock, it_clock, it_id and
125 * it_pid fields are not modified by timer code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
127 * Permissions: It is assumed that the clock_settime() function defined
128 * for each clock will take care of permission checks. Some
129 * clocks may be set able by any user (i.e. local process
130 * clocks) others not. Currently the only set able clock we
131 * have is CLOCK_REALTIME and its high res counter part, both of
132 * which we beg off on and pass to do_sys_settimeofday().
133 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700134static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
135
136#define lock_timer(tid, flags) \
137({ struct k_itimer *__timr; \
138 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
139 __timr; \
140})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400142static int hash(struct signal_struct *sig, unsigned int nr)
143{
144 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
145}
146
147static struct k_itimer *__posix_timers_find(struct hlist_head *head,
148 struct signal_struct *sig,
149 timer_t id)
150{
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400151 struct k_itimer *timer;
152
153 hlist_for_each_entry_rcu(timer, head, t_hash) {
154 if ((timer->it_signal == sig) && (timer->it_id == id))
155 return timer;
156 }
157 return NULL;
158}
159
160static struct k_itimer *posix_timer_by_id(timer_t id)
161{
162 struct signal_struct *sig = current->signal;
163 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
164
165 return __posix_timers_find(head, sig, id);
166}
167
168static int posix_timer_add(struct k_itimer *timer)
169{
170 struct signal_struct *sig = current->signal;
171 int first_free_id = sig->posix_timer_id;
172 struct hlist_head *head;
173 int ret = -ENOENT;
174
175 do {
176 spin_lock(&hash_lock);
177 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
178 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
179 hlist_add_head_rcu(&timer->t_hash, head);
180 ret = sig->posix_timer_id;
181 }
182 if (++sig->posix_timer_id < 0)
183 sig->posix_timer_id = 0;
184 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
185 /* Loop over all possible ids completed */
186 ret = -EAGAIN;
187 spin_unlock(&hash_lock);
188 } while (ret == -ENOENT);
189 return ret;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
193{
194 spin_unlock_irqrestore(&timr->it_lock, flags);
195}
196
Thomas Gleixner42285772011-02-01 13:51:50 +0000197/* Get clock_realtime */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700198static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner42285772011-02-01 13:51:50 +0000199{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700200 ktime_get_real_ts64(tp);
Thomas Gleixner42285772011-02-01 13:51:50 +0000201 return 0;
202}
203
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000204/* Set clock_realtime */
205static int posix_clock_realtime_set(const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700206 const struct timespec64 *tp)
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000207{
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700208 return do_sys_settimeofday64(tp, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000209}
210
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000211static int posix_clock_realtime_adj(const clockid_t which_clock,
212 struct timex *t)
213{
214 return do_adjtimex(t);
215}
216
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800217/*
218 * Get monotonic time for posix timers
219 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700220static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800221{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700222 ktime_get_ts64(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800223 return 0;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800227 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700228 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700229static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700230{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700231 getrawmonotonic64(tp);
John Stultz2d422442008-08-20 16:37:30 -0700232 return 0;
233}
234
john stultzda15cfd2009-08-19 19:13:34 -0700235
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700236static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700237{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700238 *tp = current_kernel_time64();
john stultzda15cfd2009-08-19 19:13:34 -0700239 return 0;
240}
241
242static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700243 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700244{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700245 *tp = get_monotonic_coarse64();
john stultzda15cfd2009-08-19 19:13:34 -0700246 return 0;
247}
248
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700249static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700250{
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700251 *tp = ktime_to_timespec64(KTIME_LOW_RES);
john stultzda15cfd2009-08-19 19:13:34 -0700252 return 0;
253}
John Stultz7fdd7f82011-02-15 10:52:57 -0800254
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +0200255static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700256{
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +0200257 get_monotonic_boottime64(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700258 return 0;
259}
John Stultz7fdd7f82011-02-15 10:52:57 -0800260
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +0200261static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner72199322018-03-01 17:33:32 +0100262{
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +0200263 timekeeping_clocktai64(tp);
Thomas Gleixner72199322018-03-01 17:33:32 +0100264 return 0;
265}
266
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700267static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000268{
269 tp->tv_sec = 0;
270 tp->tv_nsec = hrtimer_resolution;
271 return 0;
272}
273
John Stultz2d422442008-08-20 16:37:30 -0700274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Initialize everything, well, just everything in Posix clocks/timers ;)
276 */
277static __init int init_posix_timers(void)
278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700280 sizeof (struct k_itimer), 0, SLAB_PANIC,
281 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284__initcall(init_posix_timers);
285
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200286static void common_hrtimer_rearm(struct k_itimer *timr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Roman Zippel44f21472006-03-26 01:38:06 -0800288 struct hrtimer *timer = &timr->it.real.timer;
289
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200290 if (!timr->it_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return;
292
Davide Libenzi4d672e72008-02-04 22:27:26 -0800293 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
294 timer->base->get_time(),
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200295 timr->it_interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800296 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299/*
300 * This function is exported for use by the signal deliver code. It is
301 * called just prior to the info block being released and passes that
302 * block to us. It's function is to update the overrun entry AND to
303 * restart the timer. It should only be called if the timer is to be
304 * restarted (i.e. we have flagged this in the sys_private entry of the
305 * info block).
306 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300307 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * we require that the it_requeue_pending flag be set.
309 */
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200310void posixtimer_rearm(struct siginfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct k_itimer *timr;
313 unsigned long flags;
314
315 timr = lock_timer(info->si_tid, &flags);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200316 if (!timr)
317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200319 if (timr->it_requeue_pending == info->si_sys_private) {
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200320 timr->kclock->timer_rearm(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200322 timr->it_active = 1;
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200323 timr->it_overrun_last = timr->it_overrun;
324 timr->it_overrun = -1;
325 ++timr->it_requeue_pending;
326
Oleg Nesterov54da1172008-07-23 20:52:05 +0400327 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800328 }
329
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200330 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Oleg Nesterovba661292008-07-23 20:52:05 +0400333int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800335 struct task_struct *task;
336 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400337 /*
338 * FIXME: if ->sigq is queued we can race with
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200339 * dequeue_signal()->posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400340 *
341 * If dequeue_signal() sees the "right" value of
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200342 * si_sys_private it calls posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400343 * We re-queue ->sigq and drop ->it_lock().
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200344 * posixtimer_rearm() locks the timer
Oleg Nesterovba661292008-07-23 20:52:05 +0400345 * and re-schedules it while ->sigq is pending.
346 * Not really bad, but not that we want.
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Oleg Nesterov27af4242008-12-01 14:18:13 -0800350 rcu_read_lock();
351 task = pid_task(timr->it_pid, PIDTYPE_PID);
352 if (task) {
353 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
354 ret = send_sigqueue(timr->sigq, task, shared);
355 }
356 rcu_read_unlock();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700357 /* If we failed to send the signal the timer stops. */
358 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/*
362 * This function gets called when a POSIX.1b interval timer expires. It
363 * is used as a callback from the kernel internal timer. The
364 * run_timer_list code ALWAYS calls with interrupts on.
365
366 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
367 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800368static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Roman Zippel05cfb612006-03-26 01:38:12 -0800370 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800372 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800373 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Roman Zippel05cfb612006-03-26 01:38:12 -0800375 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800377
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200378 timr->it_active = 0;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200379 if (timr->it_interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800380 si_private = ++timr->it_requeue_pending;
381
382 if (posix_timer_event(timr, si_private)) {
383 /*
384 * signal was not sent because of sig_ignor
385 * we will not get a call back to restart it AND
386 * it should be restarted.
387 */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200388 if (timr->it_interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000389 ktime_t now = hrtimer_cb_get_time(timer);
390
391 /*
392 * FIXME: What we really want, is to stop this
393 * timer completely and restart it in case the
394 * SIG_IGN is removed. This is a non trivial
395 * change which involves sighand locking
396 * (sigh !), which we don't want to do late in
397 * the release cycle.
398 *
399 * For now we just let timers with an interval
400 * less than a jiffie expire every jiffie to
401 * avoid softirq starvation in case of SIG_IGN
402 * and a very small interval, which would put
403 * the timer right back on the softirq pending
404 * list. By moving now ahead of time we trick
405 * hrtimer_forward() to expire the timer
406 * later, while we still maintain the overrun
407 * accuracy, but have some inconsistency in
408 * the timer_gettime() case. This is at least
409 * better than a starved softirq. A more
410 * complex fix which solves also another related
411 * inconsistency is already in the pipeline.
412 */
413#ifdef CONFIG_HIGH_RES_TIMERS
414 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100415 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000416
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200417 if (timr->it_interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000418 now = ktime_add(now, kj);
419 }
420#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800421 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000422 hrtimer_forward(timer, now,
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200423 timr->it_interval);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800424 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800425 ++timr->it_requeue_pending;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200426 timr->it_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800430 unlock_timer(timr, flags);
431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Oleg Nesterov27af4242008-12-01 14:18:13 -0800434static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500436 struct pid *pid = task_tgid(current);
437 struct task_struct *rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100439 switch (event->sigev_notify) {
440 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500441 pid = find_vpid(event->sigev_notify_thread_id);
442 rtn = pid_task(pid, PIDTYPE_PID);
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100443 if (!rtn || !same_thread_group(rtn, current))
444 return NULL;
445 /* FALLTHRU */
446 case SIGEV_SIGNAL:
447 case SIGEV_THREAD:
448 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
449 return NULL;
450 /* FALLTHRU */
451 case SIGEV_NONE:
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500452 return pid;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100453 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return NULL;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static struct k_itimer * alloc_posix_timer(void)
459{
460 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800461 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!tmr)
463 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
465 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700466 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Eric W. Biederman3b10db2b2017-08-18 19:56:27 -0500468 clear_siginfo(&tmr->sigq->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return tmr;
470}
471
Eric Dumazet8af08872011-05-24 11:12:58 +0200472static void k_itimer_rcu_free(struct rcu_head *head)
473{
474 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
475
476 kmem_cache_free(posix_timers_cache, tmr);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#define IT_ID_SET 1
480#define IT_ID_NOT_SET 0
481static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
482{
483 if (it_id_set) {
484 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400485 spin_lock_irqsave(&hash_lock, flags);
486 hlist_del_rcu(&tmr->t_hash);
487 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800489 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200491 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Thomas Gleixner838394f2011-02-01 13:51:58 +0000494static int common_timer_create(struct k_itimer *new_timer)
495{
496 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
497 return 0;
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/* Create a POSIX.1b interval timer. */
Al Viro24820972017-06-07 09:42:39 +0100501static int do_timer_create(clockid_t which_clock, struct sigevent *event,
502 timer_t __user *created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300504 const struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700505 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700506 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int it_id_set = IT_ID_NOT_SET;
508
Thomas Gleixner838394f2011-02-01 13:51:58 +0000509 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000511 if (!kc->timer_create)
512 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 new_timer = alloc_posix_timer();
515 if (unlikely(!new_timer))
516 return -EAGAIN;
517
518 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400519 new_timer_id = posix_timer_add(new_timer);
520 if (new_timer_id < 0) {
521 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto out;
523 }
524
525 it_id_set = IT_ID_SET;
526 new_timer->it_id = (timer_t) new_timer_id;
527 new_timer->it_clock = which_clock;
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200528 new_timer->kclock = kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 new_timer->it_overrun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Al Viro24820972017-06-07 09:42:39 +0100531 if (event) {
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700532 rcu_read_lock();
Al Viro24820972017-06-07 09:42:39 +0100533 new_timer->it_pid = get_pid(good_sigevent(event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700534 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800535 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 error = -EINVAL;
537 goto out;
538 }
Al Viro24820972017-06-07 09:42:39 +0100539 new_timer->it_sigev_notify = event->sigev_notify;
540 new_timer->sigq->info.si_signo = event->sigev_signo;
541 new_timer->sigq->info.si_value = event->sigev_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
Al Viro24820972017-06-07 09:42:39 +0100543 new_timer->it_sigev_notify = SIGEV_SIGNAL;
544 new_timer->sigq->info.si_signo = SIGALRM;
545 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
546 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800547 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Oleg Nesterov717835d2008-09-22 14:42:49 -0700550 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700551 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700552
Andrey Vagin2b08de02010-07-20 15:23:14 -0700553 if (copy_to_user(created_timer_id,
554 &new_timer_id, sizeof (new_timer_id))) {
555 error = -EFAULT;
556 goto out;
557 }
558
Thomas Gleixner838394f2011-02-01 13:51:58 +0000559 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700560 if (error)
561 goto out;
562
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700563 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800564 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700565 list_add(&new_timer->list, &current->signal->posix_timers);
566 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700567
568 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000569 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * In the case of the timer belonging to another task, after
571 * the task is unlocked, the timer is owned by the other task
572 * and may cease to exist at any time. Don't use or modify
573 * new_timer after the unlock call.
574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700576 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
578}
579
Al Viro24820972017-06-07 09:42:39 +0100580SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
581 struct sigevent __user *, timer_event_spec,
582 timer_t __user *, created_timer_id)
583{
584 if (timer_event_spec) {
585 sigevent_t event;
586
587 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
588 return -EFAULT;
589 return do_timer_create(which_clock, &event, created_timer_id);
590 }
591 return do_timer_create(which_clock, NULL, created_timer_id);
592}
593
594#ifdef CONFIG_COMPAT
595COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
596 struct compat_sigevent __user *, timer_event_spec,
597 timer_t __user *, created_timer_id)
598{
599 if (timer_event_spec) {
600 sigevent_t event;
601
602 if (get_compat_sigevent(&event, timer_event_spec))
603 return -EFAULT;
604 return do_timer_create(which_clock, &event, created_timer_id);
605 }
606 return do_timer_create(which_clock, NULL, created_timer_id);
607}
608#endif
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Locking issues: We need to protect the result of the id look up until
612 * we get the timer locked down so it is not deleted under us. The
613 * removal is done under the idr spinlock so we use that here to bridge
614 * the find to the timer lock. To avoid a dead lock, the timer id MUST
615 * be release with out holding the timer lock.
616 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700617static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200620
Tejun Heoe182bb32013-02-20 15:24:12 -0800621 /*
622 * timer_t could be any type >= int and we want to make sure any
623 * @timer_id outside positive int range fails lookup.
624 */
625 if ((unsigned long long)timer_id > INT_MAX)
626 return NULL;
627
Eric Dumazet8af08872011-05-24 11:12:58 +0200628 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400629 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200631 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800632 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200633 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700634 return timr;
635 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200636 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700637 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200638 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Oleg Nesterov31d92842008-09-22 14:42:51 -0700640 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200643static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
644{
645 struct hrtimer *timer = &timr->it.real.timer;
646
647 return __hrtimer_expires_remaining_adjusted(timer, now);
648}
649
650static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
651{
652 struct hrtimer *timer = &timr->it.real.timer;
653
654 return (int)hrtimer_forward(timer, now, timr->it_interval);
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
658 * Get the time remaining on a POSIX.1b interval timer. This function
659 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
660 * mess with irq.
661 *
662 * We have a couple of messes to clean up here. First there is the case
663 * of a timer that has a requeue pending. These timers should appear to
664 * be in the timer list with an expiry as if we were to requeue them
665 * now.
666 *
667 * The second issue is the SIGEV_NONE timer which may be active but is
668 * not really ever put in the timer list (to save system resources).
669 * This timer may be expired, and if so, we will do it here. Otherwise
670 * it is the same as a requeue pending timer WRT to what we should
671 * report.
672 */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200673void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200675 const struct k_clock *kc = timr->kclock;
Roman Zippel3b98a532006-03-26 01:38:07 -0800676 ktime_t now, remaining, iv;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200677 struct timespec64 ts64;
678 bool sig_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100680 sig_none = timr->it_sigev_notify == SIGEV_NONE;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200681 iv = timr->it_interval;
Roman Zippel3b98a532006-03-26 01:38:07 -0800682
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800683 /* interval timer ? */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200684 if (iv) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700685 cur_setting->it_interval = ktime_to_timespec64(iv);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200686 } else if (!timr->it_active) {
687 /*
688 * SIGEV_NONE oneshot timers are never queued. Check them
689 * below.
690 */
691 if (!sig_none)
692 return;
693 }
Roman Zippel3b98a532006-03-26 01:38:07 -0800694
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800695 /*
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200696 * The timespec64 based conversion is suboptimal, but it's not
697 * worth to implement yet another callback.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800698 */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200699 kc->clock_get(timr->it_clock, &ts64);
700 now = timespec64_to_ktime(ts64);
Roman Zippel3b98a532006-03-26 01:38:07 -0800701
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200702 /*
703 * When a requeue is pending or this is a SIGEV_NONE timer move the
704 * expiry time forward by intervals, so expiry is > now.
705 */
706 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
707 timr->it_overrun += kc->timer_forward(timr, now);
708
709 remaining = kc->timer_remaining(timr, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800710 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100711 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800712 /*
713 * A single shot SIGEV_NONE timer must return 0, when
714 * it is expired !
715 */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200716 if (!sig_none)
Roman Zippel3b98a532006-03-26 01:38:07 -0800717 cur_setting->it_value.tv_nsec = 1;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200718 } else {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700719 cur_setting->it_value = ktime_to_timespec64(remaining);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/* Get the time remaining on a POSIX.1b interval timer. */
Al Virob0dc1242017-06-07 09:42:36 +0100724static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000726 struct k_itimer *timr;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300727 const struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000729 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 timr = lock_timer(timer_id, &flags);
732 if (!timr)
733 return -EINVAL;
734
Al Virob0dc1242017-06-07 09:42:36 +0100735 memset(setting, 0, sizeof(*setting));
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200736 kc = timr->kclock;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000737 if (WARN_ON_ONCE(!kc || !kc->timer_get))
738 ret = -EINVAL;
739 else
Al Virob0dc1242017-06-07 09:42:36 +0100740 kc->timer_get(timr, setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 unlock_timer(timr, flags);
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800745
Al Virob0dc1242017-06-07 09:42:36 +0100746/* Get the time remaining on a POSIX.1b interval timer. */
747SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
748 struct itimerspec __user *, setting)
749{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700750 struct itimerspec64 cur_setting;
Al Virob0dc1242017-06-07 09:42:36 +0100751
Deepa Dinamani725816e2017-06-24 11:45:08 -0700752 int ret = do_timer_gettime(timer_id, &cur_setting);
Al Virob0dc1242017-06-07 09:42:36 +0100753 if (!ret) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700754 if (put_itimerspec64(&cur_setting, setting))
Al Virob0dc1242017-06-07 09:42:36 +0100755 ret = -EFAULT;
756 }
757 return ret;
758}
759
760#ifdef CONFIG_COMPAT
761COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
762 struct compat_itimerspec __user *, setting)
763{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700764 struct itimerspec64 cur_setting;
Al Virob0dc1242017-06-07 09:42:36 +0100765
Deepa Dinamani725816e2017-06-24 11:45:08 -0700766 int ret = do_timer_gettime(timer_id, &cur_setting);
Al Virob0dc1242017-06-07 09:42:36 +0100767 if (!ret) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700768 if (put_compat_itimerspec64(&cur_setting, setting))
Al Virob0dc1242017-06-07 09:42:36 +0100769 ret = -EFAULT;
770 }
771 return ret;
772}
773#endif
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
776 * Get the number of overruns of a POSIX.1b interval timer. This is to
777 * be the overrun of the timer last delivered. At the same time we are
778 * accumulating overruns on the next timer. The overrun is frozen when
779 * the signal is delivered, either at the notify time (if the info block
780 * is not queued) or at the actual delivery time (as we are informed by
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200781 * the call back to posixtimer_rearm(). So all we need to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * to pick up the frozen overrun.
783 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100784SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct k_itimer *timr;
787 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 timr = lock_timer(timer_id, &flags);
791 if (!timr)
792 return -EINVAL;
793
794 overrun = timr->it_overrun_last;
795 unlock_timer(timr, flags);
796
797 return overrun;
798}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200800static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
801 bool absolute, bool sigev_none)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800803 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800804 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200806 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
Thomas Gleixner67edab42017-06-12 19:39:49 +0200807 /*
808 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
809 * clock modifications, so they become CLOCK_MONOTONIC based under the
810 * hood. See hrtimer_init(). Update timr->kclock, so the generic
811 * functions which use timr->kclock->clock_get() work.
812 *
813 * Note: it_clock stays unmodified, because the next timer_set() might
814 * use ABSTIME, so it needs to switch back.
815 */
816 if (timr->it_clock == CLOCK_REALTIME)
817 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
818
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200819 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
820 timr->it.real.timer.function = posix_timer_fn;
821
822 if (!absolute)
823 expires = ktime_add_safe(expires, timer->base->get_time());
824 hrtimer_set_expires(timer, expires);
825
826 if (!sigev_none)
827 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
828}
829
830static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
831{
832 return hrtimer_try_to_cancel(&timr->it.real.timer);
833}
834
835/* Set a POSIX.1b interval timer. */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200836int common_timer_set(struct k_itimer *timr, int flags,
837 struct itimerspec64 *new_setting,
838 struct itimerspec64 *old_setting)
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200839{
840 const struct k_clock *kc = timr->kclock;
841 bool sigev_none;
842 ktime_t expires;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (old_setting)
845 common_timer_get(timr, old_setting);
846
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200847 /* Prevent rearming by clearing the interval */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200848 timr->it_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200850 * Careful here. On SMP systems the timer expiry function could be
851 * active and spinning on timr->it_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200853 if (kc->timer_try_to_cancel(timr) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200856 timr->it_active = 0;
857 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ~REQUEUE_PENDING;
859 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800860
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200861 /* Switch off the timer when it_value is zero */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800862 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200865 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200866 expires = timespec64_to_ktime(new_setting->it_value);
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100867 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800868
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200869 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
870 timr->it_active = !sigev_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872}
873
Al Viro1acbe772017-06-07 09:42:35 +0100874static int do_timer_settime(timer_t timer_id, int flags,
875 struct itimerspec64 *new_spec64,
876 struct itimerspec64 *old_spec64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Al Viro1acbe772017-06-07 09:42:35 +0100878 const struct k_clock *kc;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700879 struct k_itimer *timr;
Al Viro5ba25332007-10-14 19:35:50 +0100880 unsigned long flag;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700881 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Al Viro1acbe772017-06-07 09:42:35 +0100883 if (!timespec64_valid(&new_spec64->it_interval) ||
884 !timespec64_valid(&new_spec64->it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EINVAL;
886
Al Viro1acbe772017-06-07 09:42:35 +0100887 if (old_spec64)
888 memset(old_spec64, 0, sizeof(*old_spec64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889retry:
890 timr = lock_timer(timer_id, &flag);
891 if (!timr)
892 return -EINVAL;
893
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200894 kc = timr->kclock;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000895 if (WARN_ON_ONCE(!kc || !kc->timer_set))
896 error = -EINVAL;
897 else
Al Viro1acbe772017-06-07 09:42:35 +0100898 error = kc->timer_set(timr, flags, new_spec64, old_spec64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 unlock_timer(timr, flag);
901 if (error == TIMER_RETRY) {
Al Viro1acbe772017-06-07 09:42:35 +0100902 old_spec64 = NULL; // We already got the old time...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto retry;
904 }
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return error;
907}
908
Al Viro1acbe772017-06-07 09:42:35 +0100909/* Set a POSIX.1b interval timer */
910SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
911 const struct itimerspec __user *, new_setting,
912 struct itimerspec __user *, old_setting)
913{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700914 struct itimerspec64 new_spec, old_spec;
915 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
Al Viro1acbe772017-06-07 09:42:35 +0100916 int error = 0;
917
918 if (!new_setting)
919 return -EINVAL;
920
Deepa Dinamani725816e2017-06-24 11:45:08 -0700921 if (get_itimerspec64(&new_spec, new_setting))
Al Viro1acbe772017-06-07 09:42:35 +0100922 return -EFAULT;
Al Viro1acbe772017-06-07 09:42:35 +0100923
Deepa Dinamani725816e2017-06-24 11:45:08 -0700924 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
Al Viro1acbe772017-06-07 09:42:35 +0100925 if (!error && old_setting) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700926 if (put_itimerspec64(&old_spec, old_setting))
Al Viro1acbe772017-06-07 09:42:35 +0100927 error = -EFAULT;
928 }
929 return error;
930}
931
932#ifdef CONFIG_COMPAT
933COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
934 struct compat_itimerspec __user *, new,
935 struct compat_itimerspec __user *, old)
936{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700937 struct itimerspec64 new_spec, old_spec;
938 struct itimerspec64 *rtn = old ? &old_spec : NULL;
Al Viro1acbe772017-06-07 09:42:35 +0100939 int error = 0;
940
941 if (!new)
942 return -EINVAL;
Deepa Dinamani725816e2017-06-24 11:45:08 -0700943 if (get_compat_itimerspec64(&new_spec, new))
Al Viro1acbe772017-06-07 09:42:35 +0100944 return -EFAULT;
945
Deepa Dinamani725816e2017-06-24 11:45:08 -0700946 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
Al Viro1acbe772017-06-07 09:42:35 +0100947 if (!error && old) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700948 if (put_compat_itimerspec64(&old_spec, old))
Al Viro1acbe772017-06-07 09:42:35 +0100949 error = -EFAULT;
950 }
951 return error;
952}
953#endif
954
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200955int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200957 const struct k_clock *kc = timer->kclock;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700958
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200959 timer->it_interval = 0;
960 if (kc->timer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return TIMER_RETRY;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200962 timer->it_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
964}
965
966static inline int timer_delete_hook(struct k_itimer *timer)
967{
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200968 const struct k_clock *kc = timer->kclock;
Thomas Gleixner6761c672011-02-01 13:52:07 +0000969
970 if (WARN_ON_ONCE(!kc || !kc->timer_del))
971 return -EINVAL;
972 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100976SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100979 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 timer = lock_timer(timer_id, &flags);
983 if (!timer)
984 return -EINVAL;
985
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800986 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unlock_timer(timer, flags);
988 goto retry_delete;
989 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_lock(&current->sighand->siglock);
992 list_del(&timer->list);
993 spin_unlock(&current->sighand->siglock);
994 /*
995 * This keeps any tasks waiting on the spin lock from thinking
996 * they got something (see the lock code above).
997 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800998 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 unlock_timer(timer, flags);
1001 release_posix_timer(timer, IT_ID_SET);
1002 return 0;
1003}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -08001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
1006 * return timer owned by the process, used by exit_itimers
1007 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001008static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 unsigned long flags;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 spin_lock_irqsave(&timer->it_lock, flags);
1014
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -08001015 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unlock_timer(timer, flags);
1017 goto retry_delete;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 list_del(&timer->list);
1020 /*
1021 * This keeps any tasks waiting on the spin lock from thinking
1022 * they got something (see the lock code above).
1023 */
Oleg Nesterov89992102008-12-01 14:18:15 -08001024 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unlock_timer(timer, flags);
1027 release_posix_timer(timer, IT_ID_SET);
1028}
1029
1030/*
Roland McGrath25f407f2005-10-21 15:03:29 -07001031 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * references to the shared signal_struct.
1033 */
1034void exit_itimers(struct signal_struct *sig)
1035{
1036 struct k_itimer *tmr;
1037
1038 while (!list_empty(&sig->posix_timers)) {
1039 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1040 itimer_delete(tmr);
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Heiko Carstens362e9c02009-01-14 14:14:07 +01001044SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001045 const struct __kernel_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001047 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001048 struct timespec64 new_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001050 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001052
Deepa Dinamani5c499412017-06-24 11:45:05 -07001053 if (get_timespec64(&new_tp, tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return -EFAULT;
1055
Deepa Dinamani5c499412017-06-24 11:45:05 -07001056 return kc->clock_set(which_clock, &new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Heiko Carstens362e9c02009-01-14 14:14:07 +01001059SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001060 struct __kernel_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001062 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001063 struct timespec64 kernel_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 int error;
1065
Thomas Gleixner42285772011-02-01 13:51:50 +00001066 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001068
Deepa Dinamani5c499412017-06-24 11:45:05 -07001069 error = kc->clock_get(which_clock, &kernel_tp);
Thomas Gleixner42285772011-02-01 13:51:50 +00001070
Deepa Dinamani5c499412017-06-24 11:45:05 -07001071 if (!error && put_timespec64(&kernel_tp, tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 error = -EFAULT;
1073
1074 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001077SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1078 struct timex __user *, utx)
1079{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001080 const struct k_clock *kc = clockid_to_kclock(which_clock);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001081 struct timex ktx;
1082 int err;
1083
1084 if (!kc)
1085 return -EINVAL;
1086 if (!kc->clock_adj)
1087 return -EOPNOTSUPP;
1088
1089 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1090 return -EFAULT;
1091
1092 err = kc->clock_adj(which_clock, &ktx);
1093
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001094 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001095 return -EFAULT;
1096
1097 return err;
1098}
1099
Al Virod822cdc2017-06-07 09:42:38 +01001100SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001101 struct __kernel_timespec __user *, tp)
Al Virod822cdc2017-06-07 09:42:38 +01001102{
1103 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001104 struct timespec64 rtn_tp;
Al Virod822cdc2017-06-07 09:42:38 +01001105 int error;
1106
1107 if (!kc)
1108 return -EINVAL;
1109
Deepa Dinamani5c499412017-06-24 11:45:05 -07001110 error = kc->clock_getres(which_clock, &rtn_tp);
Al Virod822cdc2017-06-07 09:42:38 +01001111
Deepa Dinamani5c499412017-06-24 11:45:05 -07001112 if (!error && tp && put_timespec64(&rtn_tp, tp))
Al Virod822cdc2017-06-07 09:42:38 +01001113 error = -EFAULT;
1114
1115 return error;
1116}
1117
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001118#ifdef CONFIG_COMPAT_32BIT_TIME
Al Viro3a4d44b2017-06-07 09:42:34 +01001119
Al Virod822cdc2017-06-07 09:42:38 +01001120COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1121 struct compat_timespec __user *, tp)
1122{
1123 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001124 struct timespec64 ts;
Al Virod822cdc2017-06-07 09:42:38 +01001125
1126 if (!kc || !kc->clock_set)
1127 return -EINVAL;
1128
Deepa Dinamani5c499412017-06-24 11:45:05 -07001129 if (compat_get_timespec64(&ts, tp))
Al Virod822cdc2017-06-07 09:42:38 +01001130 return -EFAULT;
1131
Deepa Dinamani5c499412017-06-24 11:45:05 -07001132 return kc->clock_set(which_clock, &ts);
Al Virod822cdc2017-06-07 09:42:38 +01001133}
1134
1135COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1136 struct compat_timespec __user *, tp)
1137{
1138 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001139 struct timespec64 ts;
1140 int err;
Al Virod822cdc2017-06-07 09:42:38 +01001141
1142 if (!kc)
1143 return -EINVAL;
1144
Deepa Dinamani5c499412017-06-24 11:45:05 -07001145 err = kc->clock_get(which_clock, &ts);
Al Virod822cdc2017-06-07 09:42:38 +01001146
Deepa Dinamani5c499412017-06-24 11:45:05 -07001147 if (!err && compat_put_timespec64(&ts, tp))
1148 err = -EFAULT;
Al Virod822cdc2017-06-07 09:42:38 +01001149
Deepa Dinamani5c499412017-06-24 11:45:05 -07001150 return err;
Al Virod822cdc2017-06-07 09:42:38 +01001151}
1152
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001153#endif
1154
1155#ifdef CONFIG_COMPAT
1156
Al Viro3a4d44b2017-06-07 09:42:34 +01001157COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1158 struct compat_timex __user *, utp)
1159{
1160 const struct k_clock *kc = clockid_to_kclock(which_clock);
1161 struct timex ktx;
1162 int err;
1163
1164 if (!kc)
1165 return -EINVAL;
1166 if (!kc->clock_adj)
1167 return -EOPNOTSUPP;
1168
1169 err = compat_get_timex(&ktx, utp);
1170 if (err)
1171 return err;
1172
1173 err = kc->clock_adj(which_clock, &ktx);
1174
1175 if (err >= 0)
1176 err = compat_put_timex(utp, &ktx);
1177
1178 return err;
1179}
Al Viro3a4d44b2017-06-07 09:42:34 +01001180
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001181#endif
1182
1183#ifdef CONFIG_COMPAT_32BIT_TIME
1184
Al Virod822cdc2017-06-07 09:42:38 +01001185COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1186 struct compat_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001188 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001189 struct timespec64 ts;
1190 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001192 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return -EINVAL;
1194
Deepa Dinamani5c499412017-06-24 11:45:05 -07001195 err = kc->clock_getres(which_clock, &ts);
1196 if (!err && tp && compat_put_timespec64(&ts, tp))
1197 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Deepa Dinamani5c499412017-06-24 11:45:05 -07001199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
Deepa Dinamani5c499412017-06-24 11:45:05 -07001201
Al Virod822cdc2017-06-07 09:42:38 +01001202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001205 * nanosleep for monotonic and realtime clocks
1206 */
1207static int common_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001208 const struct timespec64 *rqtp)
Thomas Gleixner97735f22006-01-09 20:52:37 -08001209{
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001210 return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
Oleg Nesterov080344b2008-02-01 17:29:05 +03001211 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1212 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Heiko Carstens362e9c02009-01-14 14:14:07 +01001215SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
Deepa Dinamani01909972018-03-13 21:03:33 -07001216 const struct __kernel_timespec __user *, rqtp,
1217 struct __kernel_timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001219 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001220 struct timespec64 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001222 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001224 if (!kc->nsleep)
1225 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001227 if (get_timespec64(&t, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EFAULT;
1229
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001230 if (!timespec64_valid(&t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EINVAL;
Al Viro99e6c0e2017-06-07 09:42:30 +01001232 if (flags & TIMER_ABSTIME)
1233 rmtp = NULL;
Al Viroedbeda42017-06-07 09:42:31 +01001234 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
Al Viro99e6c0e2017-06-07 09:42:30 +01001235 current->restart_block.nanosleep.rmtp = rmtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001237 return kc->nsleep(which_clock, flags, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
Toyo Abe1711ef32006-09-29 02:00:28 -07001239
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001240#ifdef CONFIG_COMPAT_32BIT_TIME
1241
Al Viroedbeda42017-06-07 09:42:31 +01001242COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1243 struct compat_timespec __user *, rqtp,
1244 struct compat_timespec __user *, rmtp)
Toyo Abe1711ef32006-09-29 02:00:28 -07001245{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001246 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001247 struct timespec64 t;
Toyo Abe1711ef32006-09-29 02:00:28 -07001248
Al Viroedbeda42017-06-07 09:42:31 +01001249 if (!kc)
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001250 return -EINVAL;
Al Viroedbeda42017-06-07 09:42:31 +01001251 if (!kc->nsleep)
1252 return -ENANOSLEEP_NOTSUP;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001253
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001254 if (compat_get_timespec64(&t, rqtp))
Al Viroedbeda42017-06-07 09:42:31 +01001255 return -EFAULT;
1256
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001257 if (!timespec64_valid(&t))
Al Viroedbeda42017-06-07 09:42:31 +01001258 return -EINVAL;
1259 if (flags & TIMER_ABSTIME)
1260 rmtp = NULL;
1261 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1262 current->restart_block.nanosleep.compat_rmtp = rmtp;
1263
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001264 return kc->nsleep(which_clock, flags, &t);
Toyo Abe1711ef32006-09-29 02:00:28 -07001265}
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001266
Al Viroedbeda42017-06-07 09:42:31 +01001267#endif
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001268
1269static const struct k_clock clock_realtime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001270 .clock_getres = posix_get_hrtimer_res,
1271 .clock_get = posix_clock_realtime_get,
1272 .clock_set = posix_clock_realtime_set,
1273 .clock_adj = posix_clock_realtime_adj,
1274 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001275 .timer_create = common_timer_create,
1276 .timer_set = common_timer_set,
1277 .timer_get = common_timer_get,
1278 .timer_del = common_timer_del,
1279 .timer_rearm = common_hrtimer_rearm,
1280 .timer_forward = common_hrtimer_forward,
1281 .timer_remaining = common_hrtimer_remaining,
1282 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1283 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001284};
1285
1286static const struct k_clock clock_monotonic = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001287 .clock_getres = posix_get_hrtimer_res,
1288 .clock_get = posix_ktime_get_ts,
1289 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001290 .timer_create = common_timer_create,
1291 .timer_set = common_timer_set,
1292 .timer_get = common_timer_get,
1293 .timer_del = common_timer_del,
1294 .timer_rearm = common_hrtimer_rearm,
1295 .timer_forward = common_hrtimer_forward,
1296 .timer_remaining = common_hrtimer_remaining,
1297 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1298 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001299};
1300
1301static const struct k_clock clock_monotonic_raw = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001302 .clock_getres = posix_get_hrtimer_res,
1303 .clock_get = posix_get_monotonic_raw,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001304};
1305
1306static const struct k_clock clock_realtime_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001307 .clock_getres = posix_get_coarse_res,
1308 .clock_get = posix_get_realtime_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001309};
1310
1311static const struct k_clock clock_monotonic_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001312 .clock_getres = posix_get_coarse_res,
1313 .clock_get = posix_get_monotonic_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001314};
1315
1316static const struct k_clock clock_tai = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001317 .clock_getres = posix_get_hrtimer_res,
1318 .clock_get = posix_get_tai,
1319 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001320 .timer_create = common_timer_create,
1321 .timer_set = common_timer_set,
1322 .timer_get = common_timer_get,
1323 .timer_del = common_timer_del,
1324 .timer_rearm = common_hrtimer_rearm,
1325 .timer_forward = common_hrtimer_forward,
1326 .timer_remaining = common_hrtimer_remaining,
1327 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1328 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001329};
1330
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001331static const struct k_clock clock_boottime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001332 .clock_getres = posix_get_hrtimer_res,
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001333 .clock_get = posix_get_boottime,
1334 .nsleep = common_nsleep,
1335 .timer_create = common_timer_create,
1336 .timer_set = common_timer_set,
1337 .timer_get = common_timer_get,
1338 .timer_del = common_timer_del,
1339 .timer_rearm = common_hrtimer_rearm,
1340 .timer_forward = common_hrtimer_forward,
1341 .timer_remaining = common_hrtimer_remaining,
1342 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1343 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001344};
1345
1346static const struct k_clock * const posix_clocks[] = {
1347 [CLOCK_REALTIME] = &clock_realtime,
1348 [CLOCK_MONOTONIC] = &clock_monotonic,
1349 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1350 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1351 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1352 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1353 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001354 [CLOCK_BOOTTIME] = &clock_boottime,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001355 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1356 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1357 [CLOCK_TAI] = &clock_tai,
1358};
1359
1360static const struct k_clock *clockid_to_kclock(const clockid_t id)
1361{
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001362 clockid_t idx = id;
1363
1364 if (id < 0) {
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001365 return (id & CLOCKFD_MASK) == CLOCKFD ?
1366 &clock_posix_dynamic : &clock_posix_cpu;
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001367 }
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001368
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001369 if (id >= ARRAY_SIZE(posix_clocks))
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001370 return NULL;
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001371
1372 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001373}