blob: 1cd10b102c51c31305e08733bba8e8541d42c11c [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 2002-10-15 Posix Clocks & timers
4 * by George Anzinger george@mvista.com
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2002 2003 by MontaVista Software.
6 *
7 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
8 * Copyright (C) 2004 Boris Hu
9 *
Thomas Gleixner0141de72018-10-31 19:21:16 +010010 * These are all the functions necessary to implement POSIX clocks & timers
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/interrupt.h>
14#include <linux/slab.h>
15#include <linux/time.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080016#include <linux/mutex.h>
Ingo Molnar61855b62017-02-05 14:35:41 +010017#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/list.h>
21#include <linux/init.h>
22#include <linux/compiler.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040023#include <linux/hash.h>
Richard Cochran0606f422011-02-01 13:52:35 +000024#include <linux/posix-clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/posix-timers.h>
26#include <linux/syscalls.h>
27#include <linux/wait.h>
28#include <linux/workqueue.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040029#include <linux/export.h>
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040030#include <linux/hashtable.h>
Al Viroedbeda42017-06-07 09:42:31 +010031#include <linux/compat.h>
Thomas Gleixner19b558d2018-02-15 17:21:55 +010032#include <linux/nospec.h>
Andrei Vagin5a590f32019-11-12 01:27:00 +000033#include <linux/time_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000035#include "timekeeping.h"
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020036#include "posix-timers.h"
Thomas Gleixner8b094cd2014-07-16 21:04:02 +000037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040039 * Management arrays for POSIX timers. Timers are now kept in static hash table
40 * with 512 entries.
41 * Timer ids are allocated by local routine, which selects proper hash head by
42 * key, constructed from current->signal address and per signal struct counter.
43 * This keeps timer ids unique per process, but now they can intersect between
44 * processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
47/*
48 * Lets keep our timers in a slab cache :-)
49 */
Christoph Lametere18b8902006-12-06 20:33:20 -080050static struct kmem_cache *posix_timers_cache;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +040051
52static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
53static DEFINE_SPINLOCK(hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Gleixner6631fa12017-05-30 23:15:39 +020055static const struct k_clock * const posix_clocks[];
56static const struct k_clock *clockid_to_kclock(const clockid_t id);
Thomas Gleixner67edab42017-06-12 19:39:49 +020057static const struct k_clock clock_realtime, clock_monotonic;
Thomas Gleixner6631fa12017-05-30 23:15:39 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
61 * SIGEV values. Here we put out an error if this assumption fails.
62 */
63#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
64 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
65#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
66#endif
67
Thomas Gleixner65da5282011-02-01 13:51:01 +000068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * The timer ID is turned into a timer address by idr_find().
70 * Verifying a valid ID consists of:
71 *
72 * a) checking that idr_find() returns other than -1.
73 * b) checking that the timer id matches the one in the timer itself.
74 * c) that the timer owner is in the callers thread group.
75 */
76
77/*
78 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
79 * to implement others. This structure defines the various
Richard Cochran00617482011-02-01 13:52:15 +000080 * clocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * RESOLUTION: Clock resolution is used to round up timer and interval
83 * times, NOT to report clock times, which are reported with as
84 * much resolution as the system can muster. In some cases this
85 * resolution may depend on the underlying clock hardware and
86 * may not be quantifiable until run time, and only then is the
87 * necessary code is written. The standard says we should say
88 * something about this issue in the documentation...
89 *
Richard Cochran00617482011-02-01 13:52:15 +000090 * FUNCTIONS: The CLOCKs structure defines possible functions to
91 * handle various clock functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 *
Richard Cochran00617482011-02-01 13:52:15 +000093 * The standard POSIX timer management code assumes the
94 * following: 1.) The k_itimer struct (sched.h) is used for
95 * the timer. 2.) The list, it_lock, it_clock, it_id and
96 * it_pid fields are not modified by timer code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * Permissions: It is assumed that the clock_settime() function defined
99 * for each clock will take care of permission checks. Some
100 * clocks may be set able by any user (i.e. local process
101 * clocks) others not. Currently the only set able clock we
102 * have is CLOCK_REALTIME and its high res counter part, both of
103 * which we beg off on and pass to do_sys_settimeofday().
104 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700105static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
106
107#define lock_timer(tid, flags) \
108({ struct k_itimer *__timr; \
109 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
110 __timr; \
111})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400113static int hash(struct signal_struct *sig, unsigned int nr)
114{
115 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
116}
117
118static struct k_itimer *__posix_timers_find(struct hlist_head *head,
119 struct signal_struct *sig,
120 timer_t id)
121{
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400122 struct k_itimer *timer;
123
Amol Grover5fb1c2a2020-02-16 13:13:30 +0530124 hlist_for_each_entry_rcu(timer, head, t_hash,
125 lockdep_is_held(&hash_lock)) {
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400126 if ((timer->it_signal == sig) && (timer->it_id == id))
127 return timer;
128 }
129 return NULL;
130}
131
132static struct k_itimer *posix_timer_by_id(timer_t id)
133{
134 struct signal_struct *sig = current->signal;
135 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
136
137 return __posix_timers_find(head, sig, id);
138}
139
140static int posix_timer_add(struct k_itimer *timer)
141{
142 struct signal_struct *sig = current->signal;
143 int first_free_id = sig->posix_timer_id;
144 struct hlist_head *head;
145 int ret = -ENOENT;
146
147 do {
148 spin_lock(&hash_lock);
149 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
150 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
151 hlist_add_head_rcu(&timer->t_hash, head);
152 ret = sig->posix_timer_id;
153 }
154 if (++sig->posix_timer_id < 0)
155 sig->posix_timer_id = 0;
156 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
157 /* Loop over all possible ids completed */
158 ret = -EAGAIN;
159 spin_unlock(&hash_lock);
160 } while (ret == -ENOENT);
161 return ret;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
165{
166 spin_unlock_irqrestore(&timr->it_lock, flags);
167}
168
Thomas Gleixner42285772011-02-01 13:51:50 +0000169/* Get clock_realtime */
Andrei Vagineaf80192019-11-12 01:26:55 +0000170static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner42285772011-02-01 13:51:50 +0000171{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700172 ktime_get_real_ts64(tp);
Thomas Gleixner42285772011-02-01 13:51:50 +0000173 return 0;
174}
175
Andrei Vagin9c71a2e2019-11-12 01:26:58 +0000176static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
177{
178 return ktime_get_real();
179}
180
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000181/* Set clock_realtime */
182static int posix_clock_realtime_set(const clockid_t which_clock,
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700183 const struct timespec64 *tp)
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000184{
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700185 return do_sys_settimeofday64(tp, NULL);
Thomas Gleixner26f9a472011-02-01 13:51:48 +0000186}
187
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000188static int posix_clock_realtime_adj(const clockid_t which_clock,
Deepa Dinamaniead25412018-07-02 22:44:21 -0700189 struct __kernel_timex *t)
Richard Cochranf1f1d5e2011-02-01 13:52:26 +0000190{
191 return do_adjtimex(t);
192}
193
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800194/*
195 * Get monotonic time for posix timers
196 */
Andrei Vagineaf80192019-11-12 01:26:55 +0000197static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800198{
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700199 ktime_get_ts64(tp);
Andrei Vagin5a590f32019-11-12 01:27:00 +0000200 timens_add_monotonic(tp);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800201 return 0;
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Andrei Vagin9c71a2e2019-11-12 01:26:58 +0000204static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
205{
206 return ktime_get();
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
John Stultz7fdd7f82011-02-15 10:52:57 -0800210 * Get monotonic-raw time for posix timers
John Stultz2d422442008-08-20 16:37:30 -0700211 */
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700212static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
John Stultz2d422442008-08-20 16:37:30 -0700213{
Arnd Bergmann58a10452018-06-18 16:32:24 +0200214 ktime_get_raw_ts64(tp);
Andrei Vagin5a590f32019-11-12 01:27:00 +0000215 timens_add_monotonic(tp);
John Stultz2d422442008-08-20 16:37:30 -0700216 return 0;
217}
218
john stultzda15cfd2009-08-19 19:13:34 -0700219
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700220static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700221{
Arnd Bergmann58a10452018-06-18 16:32:24 +0200222 ktime_get_coarse_real_ts64(tp);
john stultzda15cfd2009-08-19 19:13:34 -0700223 return 0;
224}
225
226static int posix_get_monotonic_coarse(clockid_t which_clock,
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700227 struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700228{
Arnd Bergmann58a10452018-06-18 16:32:24 +0200229 ktime_get_coarse_ts64(tp);
Andrei Vagin5a590f32019-11-12 01:27:00 +0000230 timens_add_monotonic(tp);
john stultzda15cfd2009-08-19 19:13:34 -0700231 return 0;
232}
233
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700234static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
john stultzda15cfd2009-08-19 19:13:34 -0700235{
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700236 *tp = ktime_to_timespec64(KTIME_LOW_RES);
john stultzda15cfd2009-08-19 19:13:34 -0700237 return 0;
238}
John Stultz7fdd7f82011-02-15 10:52:57 -0800239
Andrei Vagineaf80192019-11-12 01:26:55 +0000240static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
John Stultz1ff3c962012-05-03 12:43:40 -0700241{
Arnd Bergmann58a10452018-06-18 16:32:24 +0200242 ktime_get_boottime_ts64(tp);
Andrei Vagin5a590f32019-11-12 01:27:00 +0000243 timens_add_boottime(tp);
John Stultz1ff3c962012-05-03 12:43:40 -0700244 return 0;
245}
John Stultz7fdd7f82011-02-15 10:52:57 -0800246
Andrei Vagin9c71a2e2019-11-12 01:26:58 +0000247static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
248{
249 return ktime_get_boottime();
250}
251
Andrei Vagineaf80192019-11-12 01:26:55 +0000252static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner72199322018-03-01 17:33:32 +0100253{
Arnd Bergmann58a10452018-06-18 16:32:24 +0200254 ktime_get_clocktai_ts64(tp);
Thomas Gleixner72199322018-03-01 17:33:32 +0100255 return 0;
256}
257
Andrei Vagin9c71a2e2019-11-12 01:26:58 +0000258static ktime_t posix_get_tai_ktime(clockid_t which_clock)
259{
260 return ktime_get_clocktai();
261}
262
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700263static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
Thomas Gleixner056a3ca2015-04-14 21:08:32 +0000264{
265 tp->tv_sec = 0;
266 tp->tv_nsec = hrtimer_resolution;
267 return 0;
268}
269
John Stultz2d422442008-08-20 16:37:30 -0700270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * Initialize everything, well, just everything in Posix clocks/timers ;)
272 */
273static __init int init_posix_timers(void)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 posix_timers_cache = kmem_cache_create("posix_timers_cache",
Vasily Averinc5097232021-09-02 14:55:39 -0700276 sizeof(struct k_itimer), 0,
277 SLAB_PANIC | SLAB_ACCOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280__initcall(init_posix_timers);
281
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200282/*
283 * The siginfo si_overrun field and the return value of timer_getoverrun(2)
284 * are of type int. Clamp the overrun value to INT_MAX
285 */
286static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
287{
288 s64 sum = timr->it_overrun_last + (s64)baseval;
289
290 return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
291}
292
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200293static void common_hrtimer_rearm(struct k_itimer *timr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Roman Zippel44f21472006-03-26 01:38:06 -0800295 struct hrtimer *timer = &timr->it.real.timer;
296
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200297 timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
298 timr->it_interval);
Roman Zippel44f21472006-03-26 01:38:06 -0800299 hrtimer_restart(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
303 * This function is exported for use by the signal deliver code. It is
304 * called just prior to the info block being released and passes that
305 * block to us. It's function is to update the overrun entry AND to
306 * restart the timer. It should only be called if the timer is to be
307 * restarted (i.e. we have flagged this in the sys_private entry of the
308 * info block).
309 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300310 * To protect against the timer going away while the interrupt is queued,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * we require that the it_requeue_pending flag be set.
312 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200313void posixtimer_rearm(struct kernel_siginfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct k_itimer *timr;
316 unsigned long flags;
317
318 timr = lock_timer(info->si_tid, &flags);
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200319 if (!timr)
320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Gleixner0e334db2018-12-17 13:31:05 +0100322 if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
Thomas Gleixnerf37fb0a2017-05-30 23:15:47 +0200323 timr->kclock->timer_rearm(timr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200325 timr->it_active = 1;
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200326 timr->it_overrun_last = timr->it_overrun;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200327 timr->it_overrun = -1LL;
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200328 ++timr->it_requeue_pending;
329
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200330 info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800331 }
332
Thomas Gleixneraf888d62017-05-30 23:15:42 +0200333 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Oleg Nesterovba661292008-07-23 20:52:05 +0400336int posix_timer_event(struct k_itimer *timr, int si_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Eric W. Biederman24122c72018-07-20 14:30:23 -0500338 enum pid_type type;
Colin Ian King1dae37c2021-07-21 13:01:47 +0100339 int ret;
Oleg Nesterovba661292008-07-23 20:52:05 +0400340 /*
341 * FIXME: if ->sigq is queued we can race with
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200342 * dequeue_signal()->posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400343 *
344 * If dequeue_signal() sees the "right" value of
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200345 * si_sys_private it calls posixtimer_rearm().
Oleg Nesterovba661292008-07-23 20:52:05 +0400346 * We re-queue ->sigq and drop ->it_lock().
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200347 * posixtimer_rearm() locks the timer
Oleg Nesterovba661292008-07-23 20:52:05 +0400348 * and re-schedules it while ->sigq is pending.
349 * Not really bad, but not that we want.
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 timr->sigq->info.si_sys_private = si_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Eric W. Biederman24122c72018-07-20 14:30:23 -0500353 type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
354 ret = send_sigqueue(timr->sigq, timr->it_pid, type);
Oleg Nesterov4aa73612008-09-22 14:42:46 -0700355 /* If we failed to send the signal the timer stops. */
356 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*
360 * This function gets called when a POSIX.1b interval timer expires. It
361 * is used as a callback from the kernel internal timer. The
362 * run_timer_list code ALWAYS calls with interrupts on.
363
364 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
365 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800366static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Roman Zippel05cfb612006-03-26 01:38:12 -0800368 struct k_itimer *timr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long flags;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800370 int si_private = 0;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800371 enum hrtimer_restart ret = HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Roman Zippel05cfb612006-03-26 01:38:12 -0800373 timr = container_of(timer, struct k_itimer, it.real.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_lock_irqsave(&timr->it_lock, flags);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800375
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200376 timr->it_active = 0;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200377 if (timr->it_interval != 0)
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800378 si_private = ++timr->it_requeue_pending;
379
380 if (posix_timer_event(timr, si_private)) {
381 /*
382 * signal was not sent because of sig_ignor
383 * we will not get a call back to restart it AND
384 * it should be restarted.
385 */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200386 if (timr->it_interval != 0) {
Thomas Gleixner58229a12007-06-21 20:45:15 +0000387 ktime_t now = hrtimer_cb_get_time(timer);
388
389 /*
390 * FIXME: What we really want, is to stop this
391 * timer completely and restart it in case the
392 * SIG_IGN is removed. This is a non trivial
393 * change which involves sighand locking
394 * (sigh !), which we don't want to do late in
395 * the release cycle.
396 *
397 * For now we just let timers with an interval
398 * less than a jiffie expire every jiffie to
399 * avoid softirq starvation in case of SIG_IGN
400 * and a very small interval, which would put
401 * the timer right back on the softirq pending
402 * list. By moving now ahead of time we trick
403 * hrtimer_forward() to expire the timer
404 * later, while we still maintain the overrun
405 * accuracy, but have some inconsistency in
406 * the timer_gettime() case. This is at least
407 * better than a starved softirq. A more
408 * complex fix which solves also another related
409 * inconsistency is already in the pipeline.
410 */
411#ifdef CONFIG_HIGH_RES_TIMERS
412 {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100413 ktime_t kj = NSEC_PER_SEC / HZ;
Thomas Gleixner58229a12007-06-21 20:45:15 +0000414
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200415 if (timr->it_interval < kj)
Thomas Gleixner58229a12007-06-21 20:45:15 +0000416 now = ktime_add(now, kj);
417 }
418#endif
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200419 timr->it_overrun += hrtimer_forward(timer, now,
420 timr->it_interval);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800421 ret = HRTIMER_RESTART;
Roman Zippela0a0c282006-03-16 23:04:01 -0800422 ++timr->it_requeue_pending;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200423 timr->it_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800427 unlock_timer(timr, flags);
428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Oleg Nesterov27af4242008-12-01 14:18:13 -0800431static struct pid *good_sigevent(sigevent_t * event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500433 struct pid *pid = task_tgid(current);
434 struct task_struct *rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100436 switch (event->sigev_notify) {
437 case SIGEV_SIGNAL | SIGEV_THREAD_ID:
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500438 pid = find_vpid(event->sigev_notify_thread_id);
439 rtn = pid_task(pid, PIDTYPE_PID);
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100440 if (!rtn || !same_thread_group(rtn, current))
441 return NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500442 fallthrough;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100443 case SIGEV_SIGNAL:
444 case SIGEV_THREAD:
445 if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
446 return NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500447 fallthrough;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100448 case SIGEV_NONE:
Eric W. Biederman2118e1f2018-07-21 00:00:29 -0500449 return pid;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100450 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return NULL;
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static struct k_itimer * alloc_posix_timer(void)
456{
457 struct k_itimer *tmr;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800458 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!tmr)
460 return tmr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
462 kmem_cache_free(posix_timers_cache, tmr);
Dan Carpenteraa94fbd2008-10-02 14:50:14 -0700463 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Eric W. Biederman3b10db2b2017-08-18 19:56:27 -0500465 clear_siginfo(&tmr->sigq->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return tmr;
467}
468
Eric Dumazet8af08872011-05-24 11:12:58 +0200469static void k_itimer_rcu_free(struct rcu_head *head)
470{
Sebastian Andrzej Siewior5d99b322019-07-31 00:33:54 +0200471 struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
Eric Dumazet8af08872011-05-24 11:12:58 +0200472
473 kmem_cache_free(posix_timers_cache, tmr);
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#define IT_ID_SET 1
477#define IT_ID_NOT_SET 0
478static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
479{
480 if (it_id_set) {
481 unsigned long flags;
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400482 spin_lock_irqsave(&hash_lock, flags);
483 hlist_del_rcu(&tmr->t_hash);
484 spin_unlock_irqrestore(&hash_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Oleg Nesterov89992102008-12-01 14:18:15 -0800486 put_pid(tmr->it_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 sigqueue_free(tmr->sigq);
Sebastian Andrzej Siewior5d99b322019-07-31 00:33:54 +0200488 call_rcu(&tmr->rcu, k_itimer_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Thomas Gleixner838394f2011-02-01 13:51:58 +0000491static int common_timer_create(struct k_itimer *new_timer)
492{
493 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
494 return 0;
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/* Create a POSIX.1b interval timer. */
Al Viro24820972017-06-07 09:42:39 +0100498static int do_timer_create(clockid_t which_clock, struct sigevent *event,
499 timer_t __user *created_timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300501 const struct k_clock *kc = clockid_to_kclock(which_clock);
Oleg Nesterov2cd499e2008-09-22 14:42:47 -0700502 struct k_itimer *new_timer;
Oleg Nesterovef864c92008-09-22 14:42:49 -0700503 int error, new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int it_id_set = IT_ID_NOT_SET;
505
Thomas Gleixner838394f2011-02-01 13:51:58 +0000506 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -EINVAL;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000508 if (!kc->timer_create)
509 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 new_timer = alloc_posix_timer();
512 if (unlikely(!new_timer))
513 return -EAGAIN;
514
515 spin_lock_init(&new_timer->it_lock);
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400516 new_timer_id = posix_timer_add(new_timer);
517 if (new_timer_id < 0) {
518 error = new_timer_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520 }
521
522 it_id_set = IT_ID_SET;
523 new_timer->it_id = (timer_t) new_timer_id;
524 new_timer->it_clock = which_clock;
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200525 new_timer->kclock = kc;
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200526 new_timer->it_overrun = -1LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Al Viro24820972017-06-07 09:42:39 +0100528 if (event) {
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700529 rcu_read_lock();
Al Viro24820972017-06-07 09:42:39 +0100530 new_timer->it_pid = get_pid(good_sigevent(event));
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700531 rcu_read_unlock();
Oleg Nesterov89992102008-12-01 14:18:15 -0800532 if (!new_timer->it_pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 error = -EINVAL;
534 goto out;
535 }
Al Viro24820972017-06-07 09:42:39 +0100536 new_timer->it_sigev_notify = event->sigev_notify;
537 new_timer->sigq->info.si_signo = event->sigev_signo;
538 new_timer->sigq->info.si_value = event->sigev_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else {
Al Viro24820972017-06-07 09:42:39 +0100540 new_timer->it_sigev_notify = SIGEV_SIGNAL;
541 new_timer->sigq->info.si_signo = SIGALRM;
542 memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
543 new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
Oleg Nesterov89992102008-12-01 14:18:15 -0800544 new_timer->it_pid = get_pid(task_tgid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Oleg Nesterov717835d2008-09-22 14:42:49 -0700547 new_timer->sigq->info.si_tid = new_timer->it_id;
Oleg Nesterov5a9fa732008-09-22 14:42:50 -0700548 new_timer->sigq->info.si_code = SI_TIMER;
Oleg Nesterov717835d2008-09-22 14:42:49 -0700549
Andrey Vagin2b08de02010-07-20 15:23:14 -0700550 if (copy_to_user(created_timer_id,
551 &new_timer_id, sizeof (new_timer_id))) {
552 error = -EFAULT;
553 goto out;
554 }
555
Thomas Gleixner838394f2011-02-01 13:51:58 +0000556 error = kc->timer_create(new_timer);
Andrey Vagin45e0fff2010-05-24 12:15:33 -0700557 if (error)
558 goto out;
559
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700560 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov27af4242008-12-01 14:18:13 -0800561 new_timer->it_signal = current->signal;
Oleg Nesterov36b2f042008-09-22 14:42:48 -0700562 list_add(&new_timer->list, &current->signal->posix_timers);
563 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterovef864c92008-09-22 14:42:49 -0700564
565 return 0;
Thomas Gleixner838394f2011-02-01 13:51:58 +0000566 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * In the case of the timer belonging to another task, after
568 * the task is unlocked, the timer is owned by the other task
569 * and may cease to exist at any time. Don't use or modify
570 * new_timer after the unlock call.
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572out:
Oleg Nesterovef864c92008-09-22 14:42:49 -0700573 release_posix_timer(new_timer, it_id_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return error;
575}
576
Al Viro24820972017-06-07 09:42:39 +0100577SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
578 struct sigevent __user *, timer_event_spec,
579 timer_t __user *, created_timer_id)
580{
581 if (timer_event_spec) {
582 sigevent_t event;
583
584 if (copy_from_user(&event, timer_event_spec, sizeof (event)))
585 return -EFAULT;
586 return do_timer_create(which_clock, &event, created_timer_id);
587 }
588 return do_timer_create(which_clock, NULL, created_timer_id);
589}
590
591#ifdef CONFIG_COMPAT
592COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
593 struct compat_sigevent __user *, timer_event_spec,
594 timer_t __user *, created_timer_id)
595{
596 if (timer_event_spec) {
597 sigevent_t event;
598
599 if (get_compat_sigevent(&event, timer_event_spec))
600 return -EFAULT;
601 return do_timer_create(which_clock, &event, created_timer_id);
602 }
603 return do_timer_create(which_clock, NULL, created_timer_id);
604}
605#endif
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Locking issues: We need to protect the result of the id look up until
609 * we get the timer locked down so it is not deleted under us. The
610 * removal is done under the idr spinlock so we use that here to bridge
611 * the find to the timer lock. To avoid a dead lock, the timer id MUST
612 * be release with out holding the timer lock.
613 */
Namhyung Kim20f33a02010-10-20 15:57:34 -0700614static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct k_itimer *timr;
Eric Dumazet8af08872011-05-24 11:12:58 +0200617
Tejun Heoe182bb32013-02-20 15:24:12 -0800618 /*
619 * timer_t could be any type >= int and we want to make sure any
620 * @timer_id outside positive int range fails lookup.
621 */
622 if ((unsigned long long)timer_id > INT_MAX)
623 return NULL;
624
Eric Dumazet8af08872011-05-24 11:12:58 +0200625 rcu_read_lock();
Pavel Emelyanov5ed67f02013-03-11 13:12:21 +0400626 timr = posix_timer_by_id(timer_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (timr) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200628 spin_lock_irqsave(&timr->it_lock, *flags);
Oleg Nesterov89992102008-12-01 14:18:15 -0800629 if (timr->it_signal == current->signal) {
Eric Dumazet8af08872011-05-24 11:12:58 +0200630 rcu_read_unlock();
Oleg Nesterov31d92842008-09-22 14:42:51 -0700631 return timr;
632 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200633 spin_unlock_irqrestore(&timr->it_lock, *flags);
Oleg Nesterov31d92842008-09-22 14:42:51 -0700634 }
Eric Dumazet8af08872011-05-24 11:12:58 +0200635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Oleg Nesterov31d92842008-09-22 14:42:51 -0700637 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200640static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
641{
642 struct hrtimer *timer = &timr->it.real.timer;
643
644 return __hrtimer_expires_remaining_adjusted(timer, now);
645}
646
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200647static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200648{
649 struct hrtimer *timer = &timr->it.real.timer;
650
Thomas Gleixner6fec64e2018-06-26 15:21:31 +0200651 return hrtimer_forward(timer, now, timr->it_interval);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/*
655 * Get the time remaining on a POSIX.1b interval timer. This function
656 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
657 * mess with irq.
658 *
659 * We have a couple of messes to clean up here. First there is the case
660 * of a timer that has a requeue pending. These timers should appear to
661 * be in the timer list with an expiry as if we were to requeue them
662 * now.
663 *
664 * The second issue is the SIGEV_NONE timer which may be active but is
665 * not really ever put in the timer list (to save system resources).
666 * This timer may be expired, and if so, we will do it here. Otherwise
667 * it is the same as a requeue pending timer WRT to what we should
668 * report.
669 */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200670void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200672 const struct k_clock *kc = timr->kclock;
Roman Zippel3b98a532006-03-26 01:38:07 -0800673 ktime_t now, remaining, iv;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200674 bool sig_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100676 sig_none = timr->it_sigev_notify == SIGEV_NONE;
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200677 iv = timr->it_interval;
Roman Zippel3b98a532006-03-26 01:38:07 -0800678
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800679 /* interval timer ? */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200680 if (iv) {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700681 cur_setting->it_interval = ktime_to_timespec64(iv);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200682 } else if (!timr->it_active) {
683 /*
684 * SIGEV_NONE oneshot timers are never queued. Check them
685 * below.
686 */
687 if (!sig_none)
688 return;
689 }
Roman Zippel3b98a532006-03-26 01:38:07 -0800690
Andrei Vagin198fa442019-11-12 01:26:59 +0000691 now = kc->clock_get_ktime(timr->it_clock);
Roman Zippel3b98a532006-03-26 01:38:07 -0800692
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200693 /*
694 * When a requeue is pending or this is a SIGEV_NONE timer move the
695 * expiry time forward by intervals, so expiry is > now.
696 */
697 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200698 timr->it_overrun += kc->timer_forward(timr, now);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200699
700 remaining = kc->timer_remaining(timr, now);
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800701 /* Return 0 only, when the timer is expired and not pending */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100702 if (remaining <= 0) {
Roman Zippel3b98a532006-03-26 01:38:07 -0800703 /*
704 * A single shot SIGEV_NONE timer must return 0, when
705 * it is expired !
706 */
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200707 if (!sig_none)
Roman Zippel3b98a532006-03-26 01:38:07 -0800708 cur_setting->it_value.tv_nsec = 1;
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200709 } else {
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700710 cur_setting->it_value = ktime_to_timespec64(remaining);
Thomas Gleixner91d57ba2017-05-30 23:15:50 +0200711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/* Get the time remaining on a POSIX.1b interval timer. */
Al Virob0dc1242017-06-07 09:42:36 +0100715static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000717 struct k_itimer *timr;
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300718 const struct k_clock *kc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long flags;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000720 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 timr = lock_timer(timer_id, &flags);
723 if (!timr)
724 return -EINVAL;
725
Al Virob0dc1242017-06-07 09:42:36 +0100726 memset(setting, 0, sizeof(*setting));
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200727 kc = timr->kclock;
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000728 if (WARN_ON_ONCE(!kc || !kc->timer_get))
729 ret = -EINVAL;
730 else
Al Virob0dc1242017-06-07 09:42:36 +0100731 kc->timer_get(timr, setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 unlock_timer(timr, flags);
Thomas Gleixnera7319fa2011-02-01 13:52:04 +0000734 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800736
Al Virob0dc1242017-06-07 09:42:36 +0100737/* Get the time remaining on a POSIX.1b interval timer. */
738SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700739 struct __kernel_itimerspec __user *, setting)
Al Virob0dc1242017-06-07 09:42:36 +0100740{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700741 struct itimerspec64 cur_setting;
Al Virob0dc1242017-06-07 09:42:36 +0100742
Deepa Dinamani725816e2017-06-24 11:45:08 -0700743 int ret = do_timer_gettime(timer_id, &cur_setting);
Al Virob0dc1242017-06-07 09:42:36 +0100744 if (!ret) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700745 if (put_itimerspec64(&cur_setting, setting))
Al Virob0dc1242017-06-07 09:42:36 +0100746 ret = -EFAULT;
747 }
748 return ret;
749}
750
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700751#ifdef CONFIG_COMPAT_32BIT_TIME
752
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100753SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
754 struct old_itimerspec32 __user *, setting)
Al Virob0dc1242017-06-07 09:42:36 +0100755{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700756 struct itimerspec64 cur_setting;
Al Virob0dc1242017-06-07 09:42:36 +0100757
Deepa Dinamani725816e2017-06-24 11:45:08 -0700758 int ret = do_timer_gettime(timer_id, &cur_setting);
Al Virob0dc1242017-06-07 09:42:36 +0100759 if (!ret) {
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200760 if (put_old_itimerspec32(&cur_setting, setting))
Al Virob0dc1242017-06-07 09:42:36 +0100761 ret = -EFAULT;
762 }
763 return ret;
764}
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700765
Al Virob0dc1242017-06-07 09:42:36 +0100766#endif
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
769 * Get the number of overruns of a POSIX.1b interval timer. This is to
770 * be the overrun of the timer last delivered. At the same time we are
771 * accumulating overruns on the next timer. The overrun is frozen when
772 * the signal is delivered, either at the notify time (if the info block
773 * is not queued) or at the actual delivery time (as we are informed by
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200774 * the call back to posixtimer_rearm(). So all we need to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * to pick up the frozen overrun.
776 */
Heiko Carstens362e9c02009-01-14 14:14:07 +0100777SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct k_itimer *timr;
780 int overrun;
Al Viro5ba25332007-10-14 19:35:50 +0100781 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 timr = lock_timer(timer_id, &flags);
784 if (!timr)
785 return -EINVAL;
786
Thomas Gleixner78c9c4d2018-06-26 15:21:32 +0200787 overrun = timer_overrun_to_int(timr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 unlock_timer(timr, flags);
789
790 return overrun;
791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200793static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
794 bool absolute, bool sigev_none)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800796 struct hrtimer *timer = &timr->it.real.timer;
George Anzinger7978672c2006-02-01 03:05:11 -0800797 enum hrtimer_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200799 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
Thomas Gleixner67edab42017-06-12 19:39:49 +0200800 /*
801 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
802 * clock modifications, so they become CLOCK_MONOTONIC based under the
803 * hood. See hrtimer_init(). Update timr->kclock, so the generic
Andrei Vagin9c71a2e2019-11-12 01:26:58 +0000804 * functions which use timr->kclock->clock_get_*() work.
Thomas Gleixner67edab42017-06-12 19:39:49 +0200805 *
806 * Note: it_clock stays unmodified, because the next timer_set() might
807 * use ABSTIME, so it needs to switch back.
808 */
809 if (timr->it_clock == CLOCK_REALTIME)
810 timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
811
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200812 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
813 timr->it.real.timer.function = posix_timer_fn;
814
815 if (!absolute)
816 expires = ktime_add_safe(expires, timer->base->get_time());
817 hrtimer_set_expires(timer, expires);
818
819 if (!sigev_none)
820 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
821}
822
823static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
824{
825 return hrtimer_try_to_cancel(&timr->it.real.timer);
826}
827
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200828static void common_timer_wait_running(struct k_itimer *timer)
829{
830 hrtimer_cancel_wait_running(&timer->it.real.timer);
831}
832
Frederic Weisbecker0bee3b62019-08-20 15:12:23 +0200833/*
834 * On PREEMPT_RT this prevent priority inversion against softirq kthread in
835 * case it gets preempted while executing a timer callback. See comments in
836 * hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a
837 * cpu_relax().
838 */
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200839static struct k_itimer *timer_wait_running(struct k_itimer *timer,
840 unsigned long *flags)
841{
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200842 const struct k_clock *kc = READ_ONCE(timer->kclock);
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200843 timer_t timer_id = READ_ONCE(timer->it_id);
844
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200845 /* Prevent kfree(timer) after dropping the lock */
846 rcu_read_lock();
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200847 unlock_timer(timer, *flags);
Thomas Gleixnerec8f9542019-08-02 07:35:59 +0200848
849 if (!WARN_ON_ONCE(!kc->timer_wait_running))
850 kc->timer_wait_running(timer);
851
852 rcu_read_unlock();
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200853 /* Relock the timer. It might be not longer hashed. */
854 return lock_timer(timer_id, flags);
855}
856
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200857/* Set a POSIX.1b interval timer. */
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200858int common_timer_set(struct k_itimer *timr, int flags,
859 struct itimerspec64 *new_setting,
860 struct itimerspec64 *old_setting)
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200861{
862 const struct k_clock *kc = timr->kclock;
863 bool sigev_none;
864 ktime_t expires;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (old_setting)
867 common_timer_get(timr, old_setting);
868
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200869 /* Prevent rearming by clearing the interval */
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200870 timr->it_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /*
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200872 * Careful here. On SMP systems the timer expiry function could be
873 * active and spinning on timr->it_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200875 if (kc->timer_try_to_cancel(timr) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return TIMER_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200878 timr->it_active = 0;
879 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ~REQUEUE_PENDING;
881 timr->it_overrun_last = 0;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800882
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200883 /* Switch off the timer when it_value is zero */
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800884 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Thomas Gleixner80105cd2017-05-30 23:15:43 +0200887 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200888 expires = timespec64_to_ktime(new_setting->it_value);
Andrei Vagin7da8b3a2019-11-12 01:27:03 +0000889 if (flags & TIMER_ABSTIME)
890 expires = timens_ktime_to_host(timr->it_clock, expires);
Thomas Gleixnercef31d92017-12-15 10:32:03 +0100891 sigev_none = timr->it_sigev_notify == SIGEV_NONE;
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800892
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200893 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
894 timr->it_active = !sigev_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896}
897
Thomas Gleixner21670ee2019-07-31 00:33:52 +0200898static int do_timer_settime(timer_t timer_id, int tmr_flags,
Al Viro1acbe772017-06-07 09:42:35 +0100899 struct itimerspec64 *new_spec64,
900 struct itimerspec64 *old_spec64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Al Viro1acbe772017-06-07 09:42:35 +0100902 const struct k_clock *kc;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700903 struct k_itimer *timr;
Thomas Gleixner21670ee2019-07-31 00:33:52 +0200904 unsigned long flags;
Deepa Dinamani5f252b32017-03-26 12:04:17 -0700905 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Al Viro1acbe772017-06-07 09:42:35 +0100907 if (!timespec64_valid(&new_spec64->it_interval) ||
908 !timespec64_valid(&new_spec64->it_value))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EINVAL;
910
Al Viro1acbe772017-06-07 09:42:35 +0100911 if (old_spec64)
912 memset(old_spec64, 0, sizeof(*old_spec64));
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200913
Thomas Gleixner21670ee2019-07-31 00:33:52 +0200914 timr = lock_timer(timer_id, &flags);
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200915retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!timr)
917 return -EINVAL;
918
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200919 kc = timr->kclock;
Thomas Gleixner27722df2011-02-01 13:52:01 +0000920 if (WARN_ON_ONCE(!kc || !kc->timer_set))
921 error = -EINVAL;
922 else
Thomas Gleixner21670ee2019-07-31 00:33:52 +0200923 error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (error == TIMER_RETRY) {
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200926 // We already got the old time...
927 old_spec64 = NULL;
928 /* Unlocks and relocks the timer if it still exists */
929 timr = timer_wait_running(timr, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto retry;
931 }
Thomas Gleixner6945e5c2019-07-31 00:33:53 +0200932 unlock_timer(timr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return error;
935}
936
Al Viro1acbe772017-06-07 09:42:35 +0100937/* Set a POSIX.1b interval timer */
938SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700939 const struct __kernel_itimerspec __user *, new_setting,
940 struct __kernel_itimerspec __user *, old_setting)
Al Viro1acbe772017-06-07 09:42:35 +0100941{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700942 struct itimerspec64 new_spec, old_spec;
943 struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
Al Viro1acbe772017-06-07 09:42:35 +0100944 int error = 0;
945
946 if (!new_setting)
947 return -EINVAL;
948
Deepa Dinamani725816e2017-06-24 11:45:08 -0700949 if (get_itimerspec64(&new_spec, new_setting))
Al Viro1acbe772017-06-07 09:42:35 +0100950 return -EFAULT;
Al Viro1acbe772017-06-07 09:42:35 +0100951
Deepa Dinamani725816e2017-06-24 11:45:08 -0700952 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
Al Viro1acbe772017-06-07 09:42:35 +0100953 if (!error && old_setting) {
Deepa Dinamani725816e2017-06-24 11:45:08 -0700954 if (put_itimerspec64(&old_spec, old_setting))
Al Viro1acbe772017-06-07 09:42:35 +0100955 error = -EFAULT;
956 }
957 return error;
958}
959
Deepa Dinamani6ff84732018-06-16 22:11:44 -0700960#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100961SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
962 struct old_itimerspec32 __user *, new,
963 struct old_itimerspec32 __user *, old)
Al Viro1acbe772017-06-07 09:42:35 +0100964{
Deepa Dinamani725816e2017-06-24 11:45:08 -0700965 struct itimerspec64 new_spec, old_spec;
966 struct itimerspec64 *rtn = old ? &old_spec : NULL;
Al Viro1acbe772017-06-07 09:42:35 +0100967 int error = 0;
968
969 if (!new)
970 return -EINVAL;
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200971 if (get_old_itimerspec32(&new_spec, new))
Al Viro1acbe772017-06-07 09:42:35 +0100972 return -EFAULT;
973
Deepa Dinamani725816e2017-06-24 11:45:08 -0700974 error = do_timer_settime(timer_id, flags, &new_spec, rtn);
Al Viro1acbe772017-06-07 09:42:35 +0100975 if (!error && old) {
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200976 if (put_old_itimerspec32(&old_spec, old))
Al Viro1acbe772017-06-07 09:42:35 +0100977 error = -EFAULT;
978 }
979 return error;
980}
981#endif
982
Thomas Gleixnerf2c45802017-05-30 23:15:59 +0200983int common_timer_del(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200985 const struct k_clock *kc = timer->kclock;
Oleg Nesterovf972be32005-06-23 00:09:00 -0700986
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +0200987 timer->it_interval = 0;
988 if (kc->timer_try_to_cancel(timer) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return TIMER_RETRY;
Thomas Gleixner21e55c12017-05-30 23:15:48 +0200990 timer->it_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return 0;
992}
993
994static inline int timer_delete_hook(struct k_itimer *timer)
995{
Thomas Gleixnerd97bb752017-05-30 23:15:44 +0200996 const struct k_clock *kc = timer->kclock;
Thomas Gleixner6761c672011-02-01 13:52:07 +0000997
998 if (WARN_ON_ONCE(!kc || !kc->timer_del))
999 return -EINVAL;
1000 return kc->timer_del(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003/* Delete a POSIX.1b interval timer. */
Heiko Carstens362e9c02009-01-14 14:14:07 +01001004SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct k_itimer *timer;
Al Viro5ba25332007-10-14 19:35:50 +01001007 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 timer = lock_timer(timer_id, &flags);
Thomas Gleixner6945e5c2019-07-31 00:33:53 +02001010
1011retry_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!timer)
1013 return -EINVAL;
1014
Thomas Gleixner6945e5c2019-07-31 00:33:53 +02001015 if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
1016 /* Unlocks and relocks the timer if it still exists */
1017 timer = timer_wait_running(timer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto retry_delete;
1019 }
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -08001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_lock(&current->sighand->siglock);
1022 list_del(&timer->list);
1023 spin_unlock(&current->sighand->siglock);
1024 /*
1025 * This keeps any tasks waiting on the spin lock from thinking
1026 * they got something (see the lock code above).
1027 */
Oleg Nesterov89992102008-12-01 14:18:15 -08001028 timer->it_signal = NULL;
Oleg Nesterov4b7a1302008-07-25 01:47:26 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unlock_timer(timer, flags);
1031 release_posix_timer(timer, IT_ID_SET);
1032 return 0;
1033}
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -08001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*
1036 * return timer owned by the process, used by exit_itimers
1037 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001038static void itimer_delete(struct k_itimer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040retry_delete:
Sebastian Andrzej Siewior7586add2019-06-21 16:36:43 +02001041 spin_lock_irq(&timer->it_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -08001043 if (timer_delete_hook(timer) == TIMER_RETRY) {
Sebastian Andrzej Siewior7586add2019-06-21 16:36:43 +02001044 spin_unlock_irq(&timer->it_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto retry_delete;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 list_del(&timer->list);
Oleg Nesterov4b7a1302008-07-25 01:47:26 -07001048
Sebastian Andrzej Siewior7586add2019-06-21 16:36:43 +02001049 spin_unlock_irq(&timer->it_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 release_posix_timer(timer, IT_ID_SET);
1051}
1052
1053/*
Roland McGrath25f407f2005-10-21 15:03:29 -07001054 * This is called by do_exit or de_thread, only when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * references to the shared signal_struct.
1056 */
1057void exit_itimers(struct signal_struct *sig)
1058{
1059 struct k_itimer *tmr;
1060
1061 while (!list_empty(&sig->posix_timers)) {
1062 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1063 itimer_delete(tmr);
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Heiko Carstens362e9c02009-01-14 14:14:07 +01001067SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001068 const struct __kernel_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001070 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001071 struct timespec64 new_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001073 if (!kc || !kc->clock_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EINVAL;
Thomas Gleixner26f9a472011-02-01 13:51:48 +00001075
Deepa Dinamani5c499412017-06-24 11:45:05 -07001076 if (get_timespec64(&new_tp, tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EFAULT;
1078
Deepa Dinamani5c499412017-06-24 11:45:05 -07001079 return kc->clock_set(which_clock, &new_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Heiko Carstens362e9c02009-01-14 14:14:07 +01001082SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001083 struct __kernel_timespec __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001085 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001086 struct timespec64 kernel_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int error;
1088
Thomas Gleixner42285772011-02-01 13:51:50 +00001089 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -EINVAL;
Thomas Gleixner42285772011-02-01 13:51:50 +00001091
Andrei Vagin819a95f2019-11-12 01:26:54 +00001092 error = kc->clock_get_timespec(which_clock, &kernel_tp);
Thomas Gleixner42285772011-02-01 13:51:50 +00001093
Deepa Dinamani5c499412017-06-24 11:45:05 -07001094 if (!error && put_timespec64(&kernel_tp, tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 error = -EFAULT;
1096
1097 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Deepa Dinamaniead25412018-07-02 22:44:21 -07001100int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001101{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001102 const struct k_clock *kc = clockid_to_kclock(which_clock);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001103
1104 if (!kc)
1105 return -EINVAL;
1106 if (!kc->clock_adj)
1107 return -EOPNOTSUPP;
1108
Arnd Bergmann1a596392019-01-03 21:12:39 +01001109 return kc->clock_adj(which_clock, ktx);
1110}
1111
1112SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
Deepa Dinamani3876ced2018-07-02 22:44:22 -07001113 struct __kernel_timex __user *, utx)
Arnd Bergmann1a596392019-01-03 21:12:39 +01001114{
Deepa Dinamaniead25412018-07-02 22:44:21 -07001115 struct __kernel_timex ktx;
Arnd Bergmann1a596392019-01-03 21:12:39 +01001116 int err;
1117
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001118 if (copy_from_user(&ktx, utx, sizeof(ktx)))
1119 return -EFAULT;
1120
Arnd Bergmann1a596392019-01-03 21:12:39 +01001121 err = do_clock_adjtime(which_clock, &ktx);
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001122
Miroslav Lichvarf0dbe812013-01-11 11:58:58 +01001123 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
Richard Cochranf1f1d5e2011-02-01 13:52:26 +00001124 return -EFAULT;
1125
1126 return err;
1127}
1128
Al Virod822cdc2017-06-07 09:42:38 +01001129SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
Deepa Dinamani6d5b8412018-03-13 21:03:32 -07001130 struct __kernel_timespec __user *, tp)
Al Virod822cdc2017-06-07 09:42:38 +01001131{
1132 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001133 struct timespec64 rtn_tp;
Al Virod822cdc2017-06-07 09:42:38 +01001134 int error;
1135
1136 if (!kc)
1137 return -EINVAL;
1138
Deepa Dinamani5c499412017-06-24 11:45:05 -07001139 error = kc->clock_getres(which_clock, &rtn_tp);
Al Virod822cdc2017-06-07 09:42:38 +01001140
Deepa Dinamani5c499412017-06-24 11:45:05 -07001141 if (!error && tp && put_timespec64(&rtn_tp, tp))
Al Virod822cdc2017-06-07 09:42:38 +01001142 error = -EFAULT;
1143
1144 return error;
1145}
1146
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001147#ifdef CONFIG_COMPAT_32BIT_TIME
Al Viro3a4d44b2017-06-07 09:42:34 +01001148
Arnd Bergmann8dabe722019-01-07 00:33:08 +01001149SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
1150 struct old_timespec32 __user *, tp)
Al Virod822cdc2017-06-07 09:42:38 +01001151{
1152 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001153 struct timespec64 ts;
Al Virod822cdc2017-06-07 09:42:38 +01001154
1155 if (!kc || !kc->clock_set)
1156 return -EINVAL;
1157
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02001158 if (get_old_timespec32(&ts, tp))
Al Virod822cdc2017-06-07 09:42:38 +01001159 return -EFAULT;
1160
Deepa Dinamani5c499412017-06-24 11:45:05 -07001161 return kc->clock_set(which_clock, &ts);
Al Virod822cdc2017-06-07 09:42:38 +01001162}
1163
Arnd Bergmann8dabe722019-01-07 00:33:08 +01001164SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
1165 struct old_timespec32 __user *, tp)
Al Virod822cdc2017-06-07 09:42:38 +01001166{
1167 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001168 struct timespec64 ts;
1169 int err;
Al Virod822cdc2017-06-07 09:42:38 +01001170
1171 if (!kc)
1172 return -EINVAL;
1173
Andrei Vagin819a95f2019-11-12 01:26:54 +00001174 err = kc->clock_get_timespec(which_clock, &ts);
Al Virod822cdc2017-06-07 09:42:38 +01001175
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02001176 if (!err && put_old_timespec32(&ts, tp))
Deepa Dinamani5c499412017-06-24 11:45:05 -07001177 err = -EFAULT;
Al Virod822cdc2017-06-07 09:42:38 +01001178
Deepa Dinamani5c499412017-06-24 11:45:05 -07001179 return err;
Al Virod822cdc2017-06-07 09:42:38 +01001180}
1181
Arnd Bergmann8dabe722019-01-07 00:33:08 +01001182SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
1183 struct old_timex32 __user *, utp)
Al Viro3a4d44b2017-06-07 09:42:34 +01001184{
Deepa Dinamaniead25412018-07-02 22:44:21 -07001185 struct __kernel_timex ktx;
Al Viro3a4d44b2017-06-07 09:42:34 +01001186 int err;
1187
Arnd Bergmann4d5f0072019-01-02 13:28:47 +01001188 err = get_old_timex32(&ktx, utp);
Al Viro3a4d44b2017-06-07 09:42:34 +01001189 if (err)
1190 return err;
1191
Arnd Bergmann1a596392019-01-03 21:12:39 +01001192 err = do_clock_adjtime(which_clock, &ktx);
Al Viro3a4d44b2017-06-07 09:42:34 +01001193
Chen Jun2d036df2021-04-14 03:04:49 +00001194 if (err >= 0 && put_old_timex32(utp, &ktx))
1195 return -EFAULT;
Al Viro3a4d44b2017-06-07 09:42:34 +01001196
1197 return err;
1198}
Al Viro3a4d44b2017-06-07 09:42:34 +01001199
Arnd Bergmann8dabe722019-01-07 00:33:08 +01001200SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
1201 struct old_timespec32 __user *, tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001203 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamani5c499412017-06-24 11:45:05 -07001204 struct timespec64 ts;
1205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Thomas Gleixnere5e542e2011-02-01 13:51:53 +00001207 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EINVAL;
1209
Deepa Dinamani5c499412017-06-24 11:45:05 -07001210 err = kc->clock_getres(which_clock, &ts);
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02001211 if (!err && tp && put_old_timespec32(&ts, tp))
Deepa Dinamani5c499412017-06-24 11:45:05 -07001212 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Deepa Dinamani5c499412017-06-24 11:45:05 -07001214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
Deepa Dinamani5c499412017-06-24 11:45:05 -07001216
Al Virod822cdc2017-06-07 09:42:38 +01001217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/*
Thomas Gleixner97735f22006-01-09 20:52:37 -08001220 * nanosleep for monotonic and realtime clocks
1221 */
1222static int common_nsleep(const clockid_t which_clock, int flags,
Thomas Gleixner938e7cf2017-06-13 23:34:33 +02001223 const struct timespec64 *rqtp)
Thomas Gleixner97735f22006-01-09 20:52:37 -08001224{
Andrei Vaginea2d1f72019-11-12 01:27:05 +00001225 ktime_t texp = timespec64_to_ktime(*rqtp);
1226
1227 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
Oleg Nesterov080344b2008-02-01 17:29:05 +03001228 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1229 which_clock);
Thomas Gleixner97735f22006-01-09 20:52:37 -08001230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Andrei Vagin1f9b37b2019-11-12 01:27:06 +00001232static int common_nsleep_timens(const clockid_t which_clock, int flags,
1233 const struct timespec64 *rqtp)
1234{
1235 ktime_t texp = timespec64_to_ktime(*rqtp);
1236
1237 if (flags & TIMER_ABSTIME)
1238 texp = timens_ktime_to_host(which_clock, texp);
1239
1240 return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
1241 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1242 which_clock);
1243}
1244
Heiko Carstens362e9c02009-01-14 14:14:07 +01001245SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
Deepa Dinamani01909972018-03-13 21:03:33 -07001246 const struct __kernel_timespec __user *, rqtp,
1247 struct __kernel_timespec __user *, rmtp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001249 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001250 struct timespec64 t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001252 if (!kc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EINVAL;
Thomas Gleixnera5cd2882011-02-01 13:51:11 +00001254 if (!kc->nsleep)
Helge Deller93cb8e22018-07-01 18:18:15 +02001255 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001257 if (get_timespec64(&t, rqtp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EFAULT;
1259
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001260 if (!timespec64_valid(&t))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -EINVAL;
Al Viro99e6c0e2017-06-07 09:42:30 +01001262 if (flags & TIMER_ABSTIME)
1263 rmtp = NULL;
Al Viroedbeda42017-06-07 09:42:31 +01001264 current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
Al Viro99e6c0e2017-06-07 09:42:30 +01001265 current->restart_block.nanosleep.rmtp = rmtp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001267 return kc->nsleep(which_clock, flags, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
Toyo Abe1711ef32006-09-29 02:00:28 -07001269
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001270#ifdef CONFIG_COMPAT_32BIT_TIME
1271
Arnd Bergmann8dabe722019-01-07 00:33:08 +01001272SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
1273 struct old_timespec32 __user *, rqtp,
1274 struct old_timespec32 __user *, rmtp)
Toyo Abe1711ef32006-09-29 02:00:28 -07001275{
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +03001276 const struct k_clock *kc = clockid_to_kclock(which_clock);
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001277 struct timespec64 t;
Toyo Abe1711ef32006-09-29 02:00:28 -07001278
Al Viroedbeda42017-06-07 09:42:31 +01001279 if (!kc)
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001280 return -EINVAL;
Al Viroedbeda42017-06-07 09:42:31 +01001281 if (!kc->nsleep)
Helge Deller93cb8e22018-07-01 18:18:15 +02001282 return -EOPNOTSUPP;
Thomas Gleixner59bd5bc2011-02-01 13:51:17 +00001283
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02001284 if (get_old_timespec32(&t, rqtp))
Al Viroedbeda42017-06-07 09:42:31 +01001285 return -EFAULT;
1286
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001287 if (!timespec64_valid(&t))
Al Viroedbeda42017-06-07 09:42:31 +01001288 return -EINVAL;
1289 if (flags & TIMER_ABSTIME)
1290 rmtp = NULL;
1291 current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1292 current->restart_block.nanosleep.compat_rmtp = rmtp;
1293
Deepa Dinamanic0edd7c2017-06-24 11:45:06 -07001294 return kc->nsleep(which_clock, flags, &t);
Toyo Abe1711ef32006-09-29 02:00:28 -07001295}
Deepa Dinamanib5793b02018-03-13 21:03:29 -07001296
Al Viroedbeda42017-06-07 09:42:31 +01001297#endif
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001298
1299static const struct k_clock clock_realtime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001300 .clock_getres = posix_get_hrtimer_res,
Andrei Vagineaf80192019-11-12 01:26:55 +00001301 .clock_get_timespec = posix_get_realtime_timespec,
Andrei Vagin9c71a2e2019-11-12 01:26:58 +00001302 .clock_get_ktime = posix_get_realtime_ktime,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001303 .clock_set = posix_clock_realtime_set,
1304 .clock_adj = posix_clock_realtime_adj,
1305 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001306 .timer_create = common_timer_create,
1307 .timer_set = common_timer_set,
1308 .timer_get = common_timer_get,
1309 .timer_del = common_timer_del,
1310 .timer_rearm = common_hrtimer_rearm,
1311 .timer_forward = common_hrtimer_forward,
1312 .timer_remaining = common_hrtimer_remaining,
1313 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
Thomas Gleixnerec8f9542019-08-02 07:35:59 +02001314 .timer_wait_running = common_timer_wait_running,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001315 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001316};
1317
1318static const struct k_clock clock_monotonic = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001319 .clock_getres = posix_get_hrtimer_res,
Andrei Vagineaf80192019-11-12 01:26:55 +00001320 .clock_get_timespec = posix_get_monotonic_timespec,
Andrei Vagin9c71a2e2019-11-12 01:26:58 +00001321 .clock_get_ktime = posix_get_monotonic_ktime,
Andrei Vagin1f9b37b2019-11-12 01:27:06 +00001322 .nsleep = common_nsleep_timens,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001323 .timer_create = common_timer_create,
1324 .timer_set = common_timer_set,
1325 .timer_get = common_timer_get,
1326 .timer_del = common_timer_del,
1327 .timer_rearm = common_hrtimer_rearm,
1328 .timer_forward = common_hrtimer_forward,
1329 .timer_remaining = common_hrtimer_remaining,
1330 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
Thomas Gleixnerec8f9542019-08-02 07:35:59 +02001331 .timer_wait_running = common_timer_wait_running,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001332 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001333};
1334
1335static const struct k_clock clock_monotonic_raw = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001336 .clock_getres = posix_get_hrtimer_res,
Andrei Vagin819a95f2019-11-12 01:26:54 +00001337 .clock_get_timespec = posix_get_monotonic_raw,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001338};
1339
1340static const struct k_clock clock_realtime_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001341 .clock_getres = posix_get_coarse_res,
Andrei Vagin819a95f2019-11-12 01:26:54 +00001342 .clock_get_timespec = posix_get_realtime_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001343};
1344
1345static const struct k_clock clock_monotonic_coarse = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001346 .clock_getres = posix_get_coarse_res,
Andrei Vagin819a95f2019-11-12 01:26:54 +00001347 .clock_get_timespec = posix_get_monotonic_coarse,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001348};
1349
1350static const struct k_clock clock_tai = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001351 .clock_getres = posix_get_hrtimer_res,
Andrei Vagin9c71a2e2019-11-12 01:26:58 +00001352 .clock_get_ktime = posix_get_tai_ktime,
Andrei Vagineaf80192019-11-12 01:26:55 +00001353 .clock_get_timespec = posix_get_tai_timespec,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001354 .nsleep = common_nsleep,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001355 .timer_create = common_timer_create,
1356 .timer_set = common_timer_set,
1357 .timer_get = common_timer_get,
1358 .timer_del = common_timer_del,
1359 .timer_rearm = common_hrtimer_rearm,
1360 .timer_forward = common_hrtimer_forward,
1361 .timer_remaining = common_hrtimer_remaining,
1362 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
Thomas Gleixnerec8f9542019-08-02 07:35:59 +02001363 .timer_wait_running = common_timer_wait_running,
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001364 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001365};
1366
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001367static const struct k_clock clock_boottime = {
Thomas Gleixnereae1c4a2017-05-30 23:15:53 +02001368 .clock_getres = posix_get_hrtimer_res,
Andrei Vagin9c71a2e2019-11-12 01:26:58 +00001369 .clock_get_ktime = posix_get_boottime_ktime,
Andrei Vagineaf80192019-11-12 01:26:55 +00001370 .clock_get_timespec = posix_get_boottime_timespec,
Andrei Vagin1f9b37b2019-11-12 01:27:06 +00001371 .nsleep = common_nsleep_timens,
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001372 .timer_create = common_timer_create,
1373 .timer_set = common_timer_set,
1374 .timer_get = common_timer_get,
1375 .timer_del = common_timer_del,
1376 .timer_rearm = common_hrtimer_rearm,
1377 .timer_forward = common_hrtimer_forward,
1378 .timer_remaining = common_hrtimer_remaining,
1379 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
Thomas Gleixnerec8f9542019-08-02 07:35:59 +02001380 .timer_wait_running = common_timer_wait_running,
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001381 .timer_arm = common_hrtimer_arm,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001382};
1383
1384static const struct k_clock * const posix_clocks[] = {
1385 [CLOCK_REALTIME] = &clock_realtime,
1386 [CLOCK_MONOTONIC] = &clock_monotonic,
1387 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1388 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1389 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1390 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1391 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
Thomas Gleixnera3ed0e432018-04-25 15:33:38 +02001392 [CLOCK_BOOTTIME] = &clock_boottime,
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001393 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1394 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1395 [CLOCK_TAI] = &clock_tai,
1396};
1397
1398static const struct k_clock *clockid_to_kclock(const clockid_t id)
1399{
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001400 clockid_t idx = id;
1401
1402 if (id < 0) {
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001403 return (id & CLOCKFD_MASK) == CLOCKFD ?
1404 &clock_posix_dynamic : &clock_posix_cpu;
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001405 }
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001406
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001407 if (id >= ARRAY_SIZE(posix_clocks))
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001408 return NULL;
Thomas Gleixner19b558d2018-02-15 17:21:55 +01001409
1410 return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
Thomas Gleixner6631fa12017-05-30 23:15:39 +02001411}