blob: e82bb1fd614e6151801a10bbc77eb82d4344b02c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000054#include "timekeeping.h"
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020055#include "posix-timers.h"
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040058 * Management arrays for POSIX timers. Timers are now kept in static hash table
59 * with 512 entries.
60 * Timer ids are allocated by local routine, which selects proper hash head by
61 * key, constructed from current->signal address and per signal struct counter.
62 * This keeps timer ids unique per process, but now they can intersect between
63 * processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
65
66/*
67 * Lets keep our timers in a slab cache :-)
68 */
Christoph Lametere18b8902006-12-06 20:33:20 -080069static struct kmem_cache *posix_timers_cache;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040070
71static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
72static DEFINE_SPINLOCK(hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Thomas Gleixner6631fa12017-05-30 23:15:39 +020074static const struct k_clock * const posix_clocks[];
75static const struct k_clock *clockid_to_kclock(const clockid_t id);
Thomas Gleixner67edab42017-06-12 19:39:49 +020076static const struct k_clock clock_realtime, clock_monotonic;
Thomas Gleixner6631fa12017-05-30 23:15:39 +020077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
80 * SIGEV values. Here we put out an error if this assumption fails.
81 */
82#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
83 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
84#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
85#endif
86
Thomas Gleixner65da5282011-02-01 13:51:01 +000087/*
88 * parisc wants ENOTSUP instead of EOPNOTSUPP
89 */
90#ifndef ENOTSUP
91# define ENANOSLEEP_NOTSUP EOPNOTSUPP
92#else
93# define ENANOSLEEP_NOTSUP ENOTSUP
94#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * The timer ID is turned into a timer address by idr_find().
98 * Verifying a valid ID consists of:
99 *
100 * a) checking that idr_find() returns other than -1.
101 * b) checking that the timer id matches the one in the timer itself.
102 * c) that the timer owner is in the callers thread group.
103 */
104
105/*
106 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
107 * to implement others. This structure defines the various
Richard Cochran00617482011-02-01 13:52:15 +0000108 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 * RESOLUTION: Clock resolution is used to round up timer and interval
111 * times, NOT to report clock times, which are reported with as
112 * much resolution as the system can muster. In some cases this
113 * resolution may depend on the underlying clock hardware and
114 * may not be quantifiable until run time, and only then is the
115 * necessary code is written. The standard says we should say
116 * something about this issue in the documentation...
117 *
Richard Cochran00617482011-02-01 13:52:15 +0000118 * FUNCTIONS: The CLOCKs structure defines possible functions to
119 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
Richard Cochran00617482011-02-01 13:52:15 +0000121 * The standard POSIX timer management code assumes the
122 * following: 1.) The k_itimer struct (sched.h) is used for
123 * the timer. 2.) The list, it_lock, it_clock, it_id and
124 * it_pid fields are not modified by timer code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 *
126 * Permissions: It is assumed that the clock_settime() function defined
127 * for each clock will take care of permission checks. Some
128 * clocks may be set able by any user (i.e. local process
129 * clocks) others not. Currently the only set able clock we
130 * have is CLOCK_REALTIME and its high res counter part, both of
131 * which we beg off on and pass to do_sys_settimeofday().
132 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700133static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
134
135#define lock_timer(tid, flags) \
136({ struct k_itimer *__timr; \
137 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
138 __timr; \
139})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400141static int hash(struct signal_struct *sig, unsigned int nr)
142{
143 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
144}
145
146static struct k_itimer *__posix_timers_find(struct hlist_head *head,
147 struct signal_struct *sig,
148 timer_t id)
149{
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400150 struct k_itimer *timer;
151
152 hlist_for_each_entry_rcu(timer, head, t_hash) {
153 if ((timer->it_signal == sig) && (timer->it_id == id))
154 return timer;
155 }
156 return NULL;
157}
158
159static struct k_itimer *posix_timer_by_id(timer_t id)
160{
161 struct signal_struct *sig = current->signal;
162 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
163
164 return __posix_timers_find(head, sig, id);
165}
166
167static int posix_timer_add(struct k_itimer *timer)
168{
169 struct signal_struct *sig = current->signal;
170 int first_free_id = sig->posix_timer_id;
171 struct hlist_head *head;
172 int ret = -ENOENT;
173
174 do {
175 spin_lock(&hash_lock);
176 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
177 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
178 hlist_add_head_rcu(&timer->t_hash, head);
179 ret = sig->posix_timer_id;
180 }
181 if (++sig->posix_timer_id < 0)
182 sig->posix_timer_id = 0;
183 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
184 /* Loop over all possible ids completed */
185 ret = -EAGAIN;
186 spin_unlock(&hash_lock);
187 } while (ret == -ENOENT);
188 return ret;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
192{
193 spin_unlock_irqrestore(&timr->it_lock, flags);
194}
195
Thomas Gleixner42285772011-02-01 13:51:50 +0000196/* Get clock_realtime */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700197static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner42285772011-02-01 13:51:50 +0000198{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700199 ktime_get_real_ts64(tp);
Thomas Gleixner42285772011-02-01 13:51:50 +0000200 return 0;
201}
202
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000203/* Set clock_realtime */
204static int posix_clock_realtime_set(const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700205 const struct timespec64 *tp)
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000206{
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700207 return do_sys_settimeofday64(tp, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000208}
209
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000210static int posix_clock_realtime_adj(const clockid_t which_clock,
211 struct timex *t)
212{
213 return do_adjtimex(t);
214}
215
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800216/*
217 * Get monotonic time for posix timers
218 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700219static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800220{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700221 ktime_get_ts64(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800222 return 0;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800226 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700227 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700228static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700229{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700230 getrawmonotonic64(tp);
John Stultz2d422442008-08-20 16:37:30 -0700231 return 0;
232}
233
john stultzda15cfd2009-08-19 19:13:34 -0700234
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700235static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700236{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700237 *tp = current_kernel_time64();
john stultzda15cfd2009-08-19 19:13:34 -0700238 return 0;
239}
240
241static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700242 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700243{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700244 *tp = get_monotonic_coarse64();
john stultzda15cfd2009-08-19 19:13:34 -0700245 return 0;
246}
247
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700248static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700249{
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700250 *tp = ktime_to_timespec64(KTIME_LOW_RES);
john stultzda15cfd2009-08-19 19:13:34 -0700251 return 0;
252}
John Stultz7fdd7f82011-02-15 10:52:57 -0800253
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700254static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
John Stultz7fdd7f82011-02-15 10:52:57 -0800255{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700256 get_monotonic_boottime64(tp);
John Stultz7fdd7f82011-02-15 10:52:57 -0800257 return 0;
258}
259
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700260static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700261{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700262 timekeeping_clocktai64(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700263 return 0;
264}
John Stultz7fdd7f82011-02-15 10:52:57 -0800265
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700266static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000267{
268 tp->tv_sec = 0;
269 tp->tv_nsec = hrtimer_resolution;
270 return 0;
271}
272
John Stultz2d422442008-08-20 16:37:30 -0700273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * Initialize everything, well, just everything in Posix clocks/timers ;)
275 */
276static __init int init_posix_timers(void)
277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Alexey Dobriyan040b5c62007-10-16 23:26:10 -0700279 sizeof (struct k_itimer), 0, SLAB_PANIC,
280 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283__initcall(init_posix_timers);
284
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200285static void common_hrtimer_rearm(struct k_itimer *timr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Roman Zippel44f21472006-03-26 01:38:06 -0800287 struct hrtimer *timer = &timr->it.real.timer;
288
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200289 if (!timr->it_interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return;
291
Davide Libenzi4d672e72008-02-04 22:27:26 -0800292 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
293 timer->base->get_time(),
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200294 timr->it_interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800295 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/*
299 * This function is exported for use by the signal deliver code. It is
300 * called just prior to the info block being released and passes that
301 * block to us. It's function is to update the overrun entry AND to
302 * restart the timer. It should only be called if the timer is to be
303 * restarted (i.e. we have flagged this in the sys_private entry of the
304 * info block).
305 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300306 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * we require that the it_requeue_pending flag be set.
308 */
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200309void posixtimer_rearm(struct siginfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct k_itimer *timr;
312 unsigned long flags;
313
314 timr = lock_timer(info->si_tid, &flags);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200315 if (!timr)
316 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200318 if (timr->it_requeue_pending == info->si_sys_private) {
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200319 timr->kclock->timer_rearm(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200321 timr->it_active = 1;
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200322 timr->it_overrun_last = timr->it_overrun;
323 timr->it_overrun = -1;
324 ++timr->it_requeue_pending;
325
Oleg Nesterov54da1172008-07-23 20:52:05 +0400326 info->si_overrun += timr->it_overrun_last;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800327 }
328
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200329 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Oleg Nesterovba661292008-07-23 20:52:05 +0400332int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Oleg Nesterov27af4242008-12-01 14:18:13 -0800334 struct task_struct *task;
335 int shared, ret = -1;
Oleg Nesterovba661292008-07-23 20:52:05 +0400336 /*
337 * FIXME: if ->sigq is queued we can race with
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200338 * dequeue_signal()->posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400339 *
340 * If dequeue_signal() sees the "right" value of
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200341 * si_sys_private it calls posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400342 * We re-queue ->sigq and drop ->it_lock().
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200343 * posixtimer_rearm() locks the timer
Oleg Nesterovba661292008-07-23 20:52:05 +0400344 * and re-schedules it while ->sigq is pending.
345 * Not really bad, but not that we want.
346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Oleg Nesterov27af4242008-12-01 14:18:13 -0800349 rcu_read_lock();
350 task = pid_task(timr->it_pid, PIDTYPE_PID);
351 if (task) {
352 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
353 ret = send_sigqueue(timr->sigq, task, shared);
354 }
355 rcu_read_unlock();
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700356 /* If we failed to send the signal the timer stops. */
357 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360/*
361 * This function gets called when a POSIX.1b interval timer expires. It
362 * is used as a callback from the kernel internal timer. The
363 * run_timer_list code ALWAYS calls with interrupts on.
364
365 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
366 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800367static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Roman Zippel05cfb612006-03-26 01:38:12 -0800369 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800371 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800372 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Roman Zippel05cfb612006-03-26 01:38:12 -0800374 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800376
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200377 timr->it_active = 0;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200378 if (timr->it_interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800379 si_private = ++timr->it_requeue_pending;
380
381 if (posix_timer_event(timr, si_private)) {
382 /*
383 * signal was not sent because of sig_ignor
384 * we will not get a call back to restart it AND
385 * it should be restarted.
386 */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200387 if (timr->it_interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000388 ktime_t now = hrtimer_cb_get_time(timer);
389
390 /*
391 * FIXME: What we really want, is to stop this
392 * timer completely and restart it in case the
393 * SIG_IGN is removed. This is a non trivial
394 * change which involves sighand locking
395 * (sigh !), which we don't want to do late in
396 * the release cycle.
397 *
398 * For now we just let timers with an interval
399 * less than a jiffie expire every jiffie to
400 * avoid softirq starvation in case of SIG_IGN
401 * and a very small interval, which would put
402 * the timer right back on the softirq pending
403 * list. By moving now ahead of time we trick
404 * hrtimer_forward() to expire the timer
405 * later, while we still maintain the overrun
406 * accuracy, but have some inconsistency in
407 * the timer_gettime() case. This is at least
408 * better than a starved softirq. A more
409 * complex fix which solves also another related
410 * inconsistency is already in the pipeline.
411 */
412#ifdef CONFIG_HIGH_RES_TIMERS
413 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100414 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000415
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200416 if (timr->it_interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000417 now = ktime_add(now, kj);
418 }
419#endif
Davide Libenzi4d672e72008-02-04 22:27:26 -0800420 timr->it_overrun += (unsigned int)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000421 hrtimer_forward(timer, now,
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200422 timr->it_interval);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800423 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800424 ++timr->it_requeue_pending;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200425 timr->it_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800429 unlock_timer(timr, flags);
430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Oleg Nesterov27af4242008-12-01 14:18:13 -0800433static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct task_struct *rtn = current->group_leader;
436
437 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
Pavel Emelyanov8dc86af2008-02-08 04:21:52 -0800438 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -0700439 !same_thread_group(rtn, current) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
441 return NULL;
442
443 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
444 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
445 return NULL;
446
Oleg Nesterov27af4242008-12-01 14:18:13 -0800447 return task_pid(rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static struct k_itimer * alloc_posix_timer(void)
451{
452 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800453 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!tmr)
455 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
457 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700458 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Oleg Nesterovba661292008-07-23 20:52:05 +0400460 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return tmr;
462}
463
Eric Dumazet8af08872011-05-24 11:12:58 +0200464static void k_itimer_rcu_free(struct rcu_head *head)
465{
466 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
467
468 kmem_cache_free(posix_timers_cache, tmr);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#define IT_ID_SET 1
472#define IT_ID_NOT_SET 0
473static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
474{
475 if (it_id_set) {
476 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400477 spin_lock_irqsave(&hash_lock, flags);
478 hlist_del_rcu(&tmr->t_hash);
479 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800481 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 sigqueue_free(tmr->sigq);
Eric Dumazet8af08872011-05-24 11:12:58 +0200483 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Thomas Gleixner838394f2011-02-01 13:51:58 +0000486static int common_timer_create(struct k_itimer *new_timer)
487{
488 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
489 return 0;
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/* Create a POSIX.1b interval timer. */
493
Heiko Carstens362e9c02009-01-14 14:14:07 +0100494SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
495 struct sigevent __user *, timer_event_spec,
496 timer_t __user *, created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300498 const struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700499 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700500 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 sigevent_t event;
502 int it_id_set = IT_ID_NOT_SET;
503
Thomas Gleixner838394f2011-02-01 13:51:58 +0000504 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000506 if (!kc->timer_create)
507 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 new_timer = alloc_posix_timer();
510 if (unlikely(!new_timer))
511 return -EAGAIN;
512
513 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400514 new_timer_id = posix_timer_add(new_timer);
515 if (new_timer_id < 0) {
516 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto out;
518 }
519
520 it_id_set = IT_ID_SET;
521 new_timer->it_id = (timer_t) new_timer_id;
522 new_timer->it_clock = which_clock;
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200523 new_timer->kclock = kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 new_timer->it_overrun = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (timer_event_spec) {
527 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
528 error = -EFAULT;
529 goto out;
530 }
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700531 rcu_read_lock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800532 new_timer->it_pid = get_pid(good_sigevent(&event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700533 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800534 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 error = -EINVAL;
536 goto out;
537 }
538 } else {
Mathias Krause6891c452014-10-04 23:06:39 +0200539 memset(&event.sigev_value, 0, sizeof(event.sigev_value));
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700540 event.sigev_notify = SIGEV_SIGNAL;
541 event.sigev_signo = SIGALRM;
542 event.sigev_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800543 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700546 new_timer->it_sigev_notify = event.sigev_notify;
547 new_timer->sigq->info.si_signo = event.sigev_signo;
548 new_timer->sigq->info.si_value = event.sigev_value;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700549 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700550 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700551
Andrey Vagin2b08de02010-07-20 15:23:14 -0700552 if (copy_to_user(created_timer_id,
553 &new_timer_id, sizeof (new_timer_id))) {
554 error = -EFAULT;
555 goto out;
556 }
557
Thomas Gleixner838394f2011-02-01 13:51:58 +0000558 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700559 if (error)
560 goto out;
561
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700562 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800563 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700564 list_add(&new_timer->list, &current->signal->posix_timers);
565 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700566
567 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000568 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * In the case of the timer belonging to another task, after
570 * the task is unlocked, the timer is owned by the other task
571 * and may cease to exist at any time. Don't use or modify
572 * new_timer after the unlock call.
573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700575 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
577}
578
579/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * Locking issues: We need to protect the result of the id look up until
581 * we get the timer locked down so it is not deleted under us. The
582 * removal is done under the idr spinlock so we use that here to bridge
583 * the find to the timer lock. To avoid a dead lock, the timer id MUST
584 * be release with out holding the timer lock.
585 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700586static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200589
Tejun Heoe182bb32013-02-20 15:24:12 -0800590 /*
591 * timer_t could be any type >= int and we want to make sure any
592 * @timer_id outside positive int range fails lookup.
593 */
594 if ((unsigned long long)timer_id > INT_MAX)
595 return NULL;
596
Eric Dumazet8af08872011-05-24 11:12:58 +0200597 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400598 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200600 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800601 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200602 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700603 return timr;
604 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200605 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700606 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Oleg Nesterov31d92842008-09-22 14:42:51 -0700609 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200612static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
613{
614 struct hrtimer *timer = &timr->it.real.timer;
615
616 return __hrtimer_expires_remaining_adjusted(timer, now);
617}
618
619static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
620{
621 struct hrtimer *timer = &timr->it.real.timer;
622
623 return (int)hrtimer_forward(timer, now, timr->it_interval);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Get the time remaining on a POSIX.1b interval timer. This function
628 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
629 * mess with irq.
630 *
631 * We have a couple of messes to clean up here. First there is the case
632 * of a timer that has a requeue pending. These timers should appear to
633 * be in the timer list with an expiry as if we were to requeue them
634 * now.
635 *
636 * The second issue is the SIGEV_NONE timer which may be active but is
637 * not really ever put in the timer list (to save system resources).
638 * This timer may be expired, and if so, we will do it here. Otherwise
639 * it is the same as a requeue pending timer WRT to what we should
640 * report.
641 */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200642void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200644 const struct k_clock *kc = timr->kclock;
Roman Zippel3b98a532006-03-26 01:38:07 -0800645 ktime_t now, remaining, iv;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200646 struct timespec64 ts64;
647 bool sig_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Thomas Gleixnerc6503be2017-06-12 17:21:26 +0200649 sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200650 iv = timr->it_interval;
Roman Zippel3b98a532006-03-26 01:38:07 -0800651
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800652 /* interval timer ? */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200653 if (iv) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700654 cur_setting->it_interval = ktime_to_timespec64(iv);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200655 } else if (!timr->it_active) {
656 /*
657 * SIGEV_NONE oneshot timers are never queued. Check them
658 * below.
659 */
660 if (!sig_none)
661 return;
662 }
Roman Zippel3b98a532006-03-26 01:38:07 -0800663
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800664 /*
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200665 * The timespec64 based conversion is suboptimal, but it's not
666 * worth to implement yet another callback.
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800667 */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200668 kc->clock_get(timr->it_clock, &ts64);
669 now = timespec64_to_ktime(ts64);
Roman Zippel3b98a532006-03-26 01:38:07 -0800670
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200671 /*
672 * When a requeue is pending or this is a SIGEV_NONE timer move the
673 * expiry time forward by intervals, so expiry is > now.
674 */
675 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
676 timr->it_overrun += kc->timer_forward(timr, now);
677
678 remaining = kc->timer_remaining(timr, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800679 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100680 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800681 /*
682 * A single shot SIGEV_NONE timer must return 0, when
683 * it is expired !
684 */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200685 if (!sig_none)
Roman Zippel3b98a532006-03-26 01:38:07 -0800686 cur_setting->it_value.tv_nsec = 1;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200687 } else {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700688 cur_setting->it_value = ktime_to_timespec64(remaining);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/* Get the time remaining on a POSIX.1b interval timer. */
Al Virob0dc1242017-06-07 09:42:36 +0100693static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000695 struct k_itimer *timr;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300696 const struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000698 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 timr = lock_timer(timer_id, &flags);
701 if (!timr)
702 return -EINVAL;
703
Al Virob0dc1242017-06-07 09:42:36 +0100704 memset(setting, 0, sizeof(*setting));
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200705 kc = timr->kclock;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000706 if (WARN_ON_ONCE(!kc || !kc->timer_get))
707 ret = -EINVAL;
708 else
Al Virob0dc1242017-06-07 09:42:36 +0100709 kc->timer_get(timr, setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 unlock_timer(timr, flags);
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800714
Al Virob0dc1242017-06-07 09:42:36 +0100715/* Get the time remaining on a POSIX.1b interval timer. */
716SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
717 struct itimerspec __user *, setting)
718{
719 struct itimerspec64 cur_setting64;
720
721 int ret = do_timer_gettime(timer_id, &cur_setting64);
722 if (!ret) {
723 struct itimerspec cur_setting;
724 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
725 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
726 ret = -EFAULT;
727 }
728 return ret;
729}
730
731#ifdef CONFIG_COMPAT
732COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
733 struct compat_itimerspec __user *, setting)
734{
735 struct itimerspec64 cur_setting64;
736
737 int ret = do_timer_gettime(timer_id, &cur_setting64);
738 if (!ret) {
739 struct itimerspec cur_setting;
740 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
741 if (put_compat_itimerspec(setting, &cur_setting))
742 ret = -EFAULT;
743 }
744 return ret;
745}
746#endif
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/*
749 * Get the number of overruns of a POSIX.1b interval timer. This is to
750 * be the overrun of the timer last delivered. At the same time we are
751 * accumulating overruns on the next timer. The overrun is frozen when
752 * the signal is delivered, either at the notify time (if the info block
753 * is not queued) or at the actual delivery time (as we are informed by
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200754 * the call back to posixtimer_rearm(). So all we need to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * to pick up the frozen overrun.
756 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100757SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct k_itimer *timr;
760 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100761 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 timr = lock_timer(timer_id, &flags);
764 if (!timr)
765 return -EINVAL;
766
767 overrun = timr->it_overrun_last;
768 unlock_timer(timr, flags);
769
770 return overrun;
771}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200773static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
774 bool absolute, bool sigev_none)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800776 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800777 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200779 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
Thomas Gleixner67edab42017-06-12 19:39:49 +0200780 /*
781 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
782 * clock modifications, so they become CLOCK_MONOTONIC based under the
783 * hood. See hrtimer_init(). Update timr->kclock, so the generic
784 * functions which use timr->kclock->clock_get() work.
785 *
786 * Note: it_clock stays unmodified, because the next timer_set() might
787 * use ABSTIME, so it needs to switch back.
788 */
789 if (timr->it_clock == CLOCK_REALTIME)
790 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
791
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200792 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
793 timr->it.real.timer.function = posix_timer_fn;
794
795 if (!absolute)
796 expires = ktime_add_safe(expires, timer->base->get_time());
797 hrtimer_set_expires(timer, expires);
798
799 if (!sigev_none)
800 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
801}
802
803static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
804{
805 return hrtimer_try_to_cancel(&timr->it.real.timer);
806}
807
808/* Set a POSIX.1b interval timer. */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200809int common_timer_set(struct k_itimer *timr, int flags,
810 struct itimerspec64 *new_setting,
811 struct itimerspec64 *old_setting)
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200812{
813 const struct k_clock *kc = timr->kclock;
814 bool sigev_none;
815 ktime_t expires;
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (old_setting)
818 common_timer_get(timr, old_setting);
819
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200820 /* Prevent rearming by clearing the interval */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200821 timr->it_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200823 * Careful here. On SMP systems the timer expiry function could be
824 * active and spinning on timr->it_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200826 if (kc->timer_try_to_cancel(timr) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200829 timr->it_active = 0;
830 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ~REQUEUE_PENDING;
832 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800833
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200834 /* Switch off the timer when it_value is zero */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800835 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200838 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200839 expires = timespec64_to_ktime(new_setting->it_value);
840 sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800841
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200842 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
843 timr->it_active = !sigev_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845}
846
Al Viro1acbe772017-06-07 09:42:35 +0100847static int do_timer_settime(timer_t timer_id, int flags,
848 struct itimerspec64 *new_spec64,
849 struct itimerspec64 *old_spec64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Al Viro1acbe772017-06-07 09:42:35 +0100851 const struct k_clock *kc;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700852 struct k_itimer *timr;
Al Viro5ba25332007-10-14 19:35:50 +0100853 unsigned long flag;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700854 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Al Viro1acbe772017-06-07 09:42:35 +0100856 if (!timespec64_valid(&new_spec64->it_interval) ||
857 !timespec64_valid(&new_spec64->it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859
Al Viro1acbe772017-06-07 09:42:35 +0100860 if (old_spec64)
861 memset(old_spec64, 0, sizeof(*old_spec64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862retry:
863 timr = lock_timer(timer_id, &flag);
864 if (!timr)
865 return -EINVAL;
866
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200867 kc = timr->kclock;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000868 if (WARN_ON_ONCE(!kc || !kc->timer_set))
869 error = -EINVAL;
870 else
Al Viro1acbe772017-06-07 09:42:35 +0100871 error = kc->timer_set(timr, flags, new_spec64, old_spec64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 unlock_timer(timr, flag);
874 if (error == TIMER_RETRY) {
Al Viro1acbe772017-06-07 09:42:35 +0100875 old_spec64 = NULL; // We already got the old time...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto retry;
877 }
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return error;
880}
881
Al Viro1acbe772017-06-07 09:42:35 +0100882/* Set a POSIX.1b interval timer */
883SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
884 const struct itimerspec __user *, new_setting,
885 struct itimerspec __user *, old_setting)
886{
887 struct itimerspec64 new_spec64, old_spec64;
888 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
889 struct itimerspec new_spec;
890 int error = 0;
891
892 if (!new_setting)
893 return -EINVAL;
894
895 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
896 return -EFAULT;
897 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
898
899 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
900 if (!error && old_setting) {
901 struct itimerspec old_spec;
902 old_spec = itimerspec64_to_itimerspec(&old_spec64);
903 if (copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
904 error = -EFAULT;
905 }
906 return error;
907}
908
909#ifdef CONFIG_COMPAT
910COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
911 struct compat_itimerspec __user *, new,
912 struct compat_itimerspec __user *, old)
913{
914 struct itimerspec64 new_spec64, old_spec64;
915 struct itimerspec64 *rtn = old ? &old_spec64 : NULL;
916 struct itimerspec new_spec;
917 int error = 0;
918
919 if (!new)
920 return -EINVAL;
921 if (get_compat_itimerspec(&new_spec, new))
922 return -EFAULT;
923
924 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
925 error = do_timer_settime(timer_id, flags, &new_spec64, rtn);
926 if (!error && old) {
927 struct itimerspec old_spec;
928 old_spec = itimerspec64_to_itimerspec(&old_spec64);
929 if (put_compat_itimerspec(old, &old_spec))
930 error = -EFAULT;
931 }
932 return error;
933}
934#endif
935
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200936int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200938 const struct k_clock *kc = timer->kclock;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700939
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200940 timer->it_interval = 0;
941 if (kc->timer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return TIMER_RETRY;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200943 timer->it_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945}
946
947static inline int timer_delete_hook(struct k_itimer *timer)
948{
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200949 const struct k_clock *kc = timer->kclock;
Thomas Gleixner6761c672011-02-01 13:52:07 +0000950
951 if (WARN_ON_ONCE(!kc || !kc->timer_del))
952 return -EINVAL;
953 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100957SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +0100960 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 timer = lock_timer(timer_id, &flags);
964 if (!timer)
965 return -EINVAL;
966
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800967 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unlock_timer(timer, flags);
969 goto retry_delete;
970 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_lock(&current->sighand->siglock);
973 list_del(&timer->list);
974 spin_unlock(&current->sighand->siglock);
975 /*
976 * This keeps any tasks waiting on the spin lock from thinking
977 * they got something (see the lock code above).
978 */
Oleg Nesterov89992102008-12-01 14:18:15 -0800979 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unlock_timer(timer, flags);
982 release_posix_timer(timer, IT_ID_SET);
983 return 0;
984}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * return timer owned by the process, used by exit_itimers
988 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800989static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 unsigned long flags;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 spin_lock_irqsave(&timer->it_lock, flags);
995
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800996 if (timer_delete_hook(timer) == TIMER_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unlock_timer(timer, flags);
998 goto retry_delete;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 list_del(&timer->list);
1001 /*
1002 * This keeps any tasks waiting on the spin lock from thinking
1003 * they got something (see the lock code above).
1004 */
Oleg Nesterov89992102008-12-01 14:18:15 -08001005 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 unlock_timer(timer, flags);
1008 release_posix_timer(timer, IT_ID_SET);
1009}
1010
1011/*
Roland McGrath25f407f2005-10-21 15:03:29 -07001012 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * references to the shared signal_struct.
1014 */
1015void exit_itimers(struct signal_struct *sig)
1016{
1017 struct k_itimer *tmr;
1018
1019 while (!list_empty(&sig->posix_timers)) {
1020 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1021 itimer_delete(tmr);
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Heiko Carstens362e9c02009-01-14 14:14:07 +01001025SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1026 const struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001028 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001029 struct timespec64 new_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct timespec new_tp;
1031
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001032 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1036 return -EFAULT;
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001037 new_tp64 = timespec_to_timespec64(new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -07001039 return kc->clock_set(which_clock, &new_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Heiko Carstens362e9c02009-01-14 14:14:07 +01001042SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1043 struct timespec __user *,tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001045 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001046 struct timespec64 kernel_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct timespec kernel_tp;
1048 int error;
1049
Thomas Gleixner42285772011-02-01 13:51:50 +00001050 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001052
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -07001053 error = kc->clock_get(which_clock, &kernel_tp64);
1054 kernel_tp = timespec64_to_timespec(kernel_tp64);
Thomas Gleixner42285772011-02-01 13:51:50 +00001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1057 error = -EFAULT;
1058
1059 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001062SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1063 struct timex __user *, utx)
1064{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001065 const struct k_clock *kc = clockid_to_kclock(which_clock);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001066 struct timex ktx;
1067 int err;
1068
1069 if (!kc)
1070 return -EINVAL;
1071 if (!kc->clock_adj)
1072 return -EOPNOTSUPP;
1073
1074 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1075 return -EFAULT;
1076
1077 err = kc->clock_adj(which_clock, &ktx);
1078
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001079 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001080 return -EFAULT;
1081
1082 return err;
1083}
1084
Al Viro3a4d44b2017-06-07 09:42:34 +01001085#ifdef CONFIG_COMPAT
1086
1087COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1088 struct compat_timex __user *, utp)
1089{
1090 const struct k_clock *kc = clockid_to_kclock(which_clock);
1091 struct timex ktx;
1092 int err;
1093
1094 if (!kc)
1095 return -EINVAL;
1096 if (!kc->clock_adj)
1097 return -EOPNOTSUPP;
1098
1099 err = compat_get_timex(&ktx, utp);
1100 if (err)
1101 return err;
1102
1103 err = kc->clock_adj(which_clock, &ktx);
1104
1105 if (err >= 0)
1106 err = compat_put_timex(utp, &ktx);
1107
1108 return err;
1109}
1110#endif
1111
Heiko Carstens362e9c02009-01-14 14:14:07 +01001112SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1113 struct timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001115 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001116 struct timespec64 rtn_tp64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct timespec rtn_tp;
1118 int error;
1119
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001120 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return -EINVAL;
1122
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -07001123 error = kc->clock_getres(which_clock, &rtn_tp64);
1124 rtn_tp = timespec64_to_timespec(rtn_tp64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001126 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 return error;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001133 * nanosleep for monotonic and realtime clocks
1134 */
1135static int common_nsleep(const clockid_t which_clock, int flags,
Al Viro99e6c0e2017-06-07 09:42:30 +01001136 struct timespec64 *tsave)
Thomas Gleixner97735f22006-01-09 20:52:37 -08001137{
Al Viro192a82f2017-06-07 09:42:28 +01001138 return hrtimer_nanosleep(tsave, flags & TIMER_ABSTIME ?
Oleg Nesterov080344b2008-02-01 17:29:05 +03001139 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1140 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001141}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Heiko Carstens362e9c02009-01-14 14:14:07 +01001143SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1144 const struct timespec __user *, rqtp,
1145 struct timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001147 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamaniad196382017-03-26 12:04:18 -07001148 struct timespec64 t64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001151 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001153 if (!kc->nsleep)
1154 return -ENANOSLEEP_NOTSUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1157 return -EFAULT;
1158
Deepa Dinamaniad196382017-03-26 12:04:18 -07001159 t64 = timespec_to_timespec64(t);
1160 if (!timespec64_valid(&t64))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -EINVAL;
Al Viro99e6c0e2017-06-07 09:42:30 +01001162 if (flags & TIMER_ABSTIME)
1163 rmtp = NULL;
Al Viroedbeda42017-06-07 09:42:31 +01001164 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
Al Viro99e6c0e2017-06-07 09:42:30 +01001165 current->restart_block.nanosleep.rmtp = rmtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Al Viro99e6c0e2017-06-07 09:42:30 +01001167 return kc->nsleep(which_clock, flags, &t64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
Toyo Abe1711ef32006-09-29 02:00:28 -07001169
Al Viroedbeda42017-06-07 09:42:31 +01001170#ifdef CONFIG_COMPAT
1171COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1172 struct compat_timespec __user *, rqtp,
1173 struct compat_timespec __user *, rmtp)
Toyo Abe1711ef32006-09-29 02:00:28 -07001174{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001175 const struct k_clock *kc = clockid_to_kclock(which_clock);
Al Viroedbeda42017-06-07 09:42:31 +01001176 struct timespec64 t64;
1177 struct timespec t;
Toyo Abe1711ef32006-09-29 02:00:28 -07001178
Al Viroedbeda42017-06-07 09:42:31 +01001179 if (!kc)
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001180 return -EINVAL;
Al Viroedbeda42017-06-07 09:42:31 +01001181 if (!kc->nsleep)
1182 return -ENANOSLEEP_NOTSUP;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001183
Al Viroedbeda42017-06-07 09:42:31 +01001184 if (compat_get_timespec(&t, rqtp))
1185 return -EFAULT;
1186
1187 t64 = timespec_to_timespec64(t);
1188 if (!timespec64_valid(&t64))
1189 return -EINVAL;
1190 if (flags & TIMER_ABSTIME)
1191 rmtp = NULL;
1192 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1193 current->restart_block.nanosleep.compat_rmtp = rmtp;
1194
1195 return kc->nsleep(which_clock, flags, &t64);
Toyo Abe1711ef32006-09-29 02:00:28 -07001196}
Al Viroedbeda42017-06-07 09:42:31 +01001197#endif
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001198
1199static const struct k_clock clock_realtime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001200 .clock_getres = posix_get_hrtimer_res,
1201 .clock_get = posix_clock_realtime_get,
1202 .clock_set = posix_clock_realtime_set,
1203 .clock_adj = posix_clock_realtime_adj,
1204 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001205 .timer_create = common_timer_create,
1206 .timer_set = common_timer_set,
1207 .timer_get = common_timer_get,
1208 .timer_del = common_timer_del,
1209 .timer_rearm = common_hrtimer_rearm,
1210 .timer_forward = common_hrtimer_forward,
1211 .timer_remaining = common_hrtimer_remaining,
1212 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1213 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001214};
1215
1216static const struct k_clock clock_monotonic = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001217 .clock_getres = posix_get_hrtimer_res,
1218 .clock_get = posix_ktime_get_ts,
1219 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001220 .timer_create = common_timer_create,
1221 .timer_set = common_timer_set,
1222 .timer_get = common_timer_get,
1223 .timer_del = common_timer_del,
1224 .timer_rearm = common_hrtimer_rearm,
1225 .timer_forward = common_hrtimer_forward,
1226 .timer_remaining = common_hrtimer_remaining,
1227 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1228 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001229};
1230
1231static const struct k_clock clock_monotonic_raw = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001232 .clock_getres = posix_get_hrtimer_res,
1233 .clock_get = posix_get_monotonic_raw,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001234};
1235
1236static const struct k_clock clock_realtime_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001237 .clock_getres = posix_get_coarse_res,
1238 .clock_get = posix_get_realtime_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001239};
1240
1241static const struct k_clock clock_monotonic_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001242 .clock_getres = posix_get_coarse_res,
1243 .clock_get = posix_get_monotonic_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001244};
1245
1246static const struct k_clock clock_tai = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001247 .clock_getres = posix_get_hrtimer_res,
1248 .clock_get = posix_get_tai,
1249 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001250 .timer_create = common_timer_create,
1251 .timer_set = common_timer_set,
1252 .timer_get = common_timer_get,
1253 .timer_del = common_timer_del,
1254 .timer_rearm = common_hrtimer_rearm,
1255 .timer_forward = common_hrtimer_forward,
1256 .timer_remaining = common_hrtimer_remaining,
1257 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1258 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001259};
1260
1261static const struct k_clock clock_boottime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001262 .clock_getres = posix_get_hrtimer_res,
1263 .clock_get = posix_get_boottime,
1264 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001265 .timer_create = common_timer_create,
1266 .timer_set = common_timer_set,
1267 .timer_get = common_timer_get,
1268 .timer_del = common_timer_del,
1269 .timer_rearm = common_hrtimer_rearm,
1270 .timer_forward = common_hrtimer_forward,
1271 .timer_remaining = common_hrtimer_remaining,
1272 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1273 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001274};
1275
1276static const struct k_clock * const posix_clocks[] = {
1277 [CLOCK_REALTIME] = &clock_realtime,
1278 [CLOCK_MONOTONIC] = &clock_monotonic,
1279 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1280 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1281 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1282 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1283 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1284 [CLOCK_BOOTTIME] = &clock_boottime,
1285 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1286 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1287 [CLOCK_TAI] = &clock_tai,
1288};
1289
1290static const struct k_clock *clockid_to_kclock(const clockid_t id)
1291{
1292 if (id < 0)
1293 return (id & CLOCKFD_MASK) == CLOCKFD ?
1294 &clock_posix_dynamic : &clock_posix_cpu;
1295
1296 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1297 return NULL;
1298 return posix_clocks[id];
1299}