• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/kernel/posix-timers.c
3  *
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>
34 #include <linux/interrupt.h>
35 #include <linux/slab.h>
36 #include <linux/time.h>
37 #include <linux/mutex.h>
38 #include <linux/sched/task.h>
39 
40 #include <linux/uaccess.h>
41 #include <linux/list.h>
42 #include <linux/init.h>
43 #include <linux/compiler.h>
44 #include <linux/hash.h>
45 #include <linux/posix-clock.h>
46 #include <linux/posix-timers.h>
47 #include <linux/syscalls.h>
48 #include <linux/wait.h>
49 #include <linux/workqueue.h>
50 #include <linux/export.h>
51 #include <linux/hashtable.h>
52 #include <linux/compat.h>
53 #include <linux/nospec.h>
54 
55 #include "timekeeping.h"
56 #include "posix-timers.h"
57 
58 /*
59  * Management arrays for POSIX timers. Timers are now kept in static hash table
60  * with 512 entries.
61  * Timer ids are allocated by local routine, which selects proper hash head by
62  * key, constructed from current->signal address and per signal struct counter.
63  * This keeps timer ids unique per process, but now they can intersect between
64  * processes.
65  */
66 
67 /*
68  * Lets keep our timers in a slab cache :-)
69  */
70 static struct kmem_cache *posix_timers_cache;
71 
72 static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
73 static DEFINE_SPINLOCK(hash_lock);
74 
75 static const struct k_clock * const posix_clocks[];
76 static const struct k_clock *clockid_to_kclock(const clockid_t id);
77 static const struct k_clock clock_realtime, clock_monotonic;
78 
79 /*
80  * we assume that the new SIGEV_THREAD_ID shares no bits with the other
81  * SIGEV values.  Here we put out an error if this assumption fails.
82  */
83 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
84                        ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
85 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
86 #endif
87 
88 /*
89  * parisc wants ENOTSUP instead of EOPNOTSUPP
90  */
91 #ifndef ENOTSUP
92 # define ENANOSLEEP_NOTSUP EOPNOTSUPP
93 #else
94 # define ENANOSLEEP_NOTSUP ENOTSUP
95 #endif
96 
97 /*
98  * The timer ID is turned into a timer address by idr_find().
99  * Verifying a valid ID consists of:
100  *
101  * a) checking that idr_find() returns other than -1.
102  * b) checking that the timer id matches the one in the timer itself.
103  * c) that the timer owner is in the callers thread group.
104  */
105 
106 /*
107  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
108  *	    to implement others.  This structure defines the various
109  *	    clocks.
110  *
111  * RESOLUTION: Clock resolution is used to round up timer and interval
112  *	    times, NOT to report clock times, which are reported with as
113  *	    much resolution as the system can muster.  In some cases this
114  *	    resolution may depend on the underlying clock hardware and
115  *	    may not be quantifiable until run time, and only then is the
116  *	    necessary code is written.	The standard says we should say
117  *	    something about this issue in the documentation...
118  *
119  * FUNCTIONS: The CLOCKs structure defines possible functions to
120  *	    handle various clock functions.
121  *
122  *	    The standard POSIX timer management code assumes the
123  *	    following: 1.) The k_itimer struct (sched.h) is used for
124  *	    the timer.  2.) The list, it_lock, it_clock, it_id and
125  *	    it_pid fields are not modified by timer code.
126  *
127  * Permissions: It is assumed that the clock_settime() function defined
128  *	    for each clock will take care of permission checks.	 Some
129  *	    clocks may be set able by any user (i.e. local process
130  *	    clocks) others not.	 Currently the only set able clock we
131  *	    have is CLOCK_REALTIME and its high res counter part, both of
132  *	    which we beg off on and pass to do_sys_settimeofday().
133  */
134 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
135 
136 #define lock_timer(tid, flags)						   \
137 ({	struct k_itimer *__timr;					   \
138 	__cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags));  \
139 	__timr;								   \
140 })
141 
hash(struct signal_struct * sig,unsigned int nr)142 static int hash(struct signal_struct *sig, unsigned int nr)
143 {
144 	return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
145 }
146 
__posix_timers_find(struct hlist_head * head,struct signal_struct * sig,timer_t id)147 static struct k_itimer *__posix_timers_find(struct hlist_head *head,
148 					    struct signal_struct *sig,
149 					    timer_t id)
150 {
151 	struct k_itimer *timer;
152 
153 	hlist_for_each_entry_rcu(timer, head, t_hash) {
154 		if ((timer->it_signal == sig) && (timer->it_id == id))
155 			return timer;
156 	}
157 	return NULL;
158 }
159 
posix_timer_by_id(timer_t id)160 static struct k_itimer *posix_timer_by_id(timer_t id)
161 {
162 	struct signal_struct *sig = current->signal;
163 	struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
164 
165 	return __posix_timers_find(head, sig, id);
166 }
167 
posix_timer_add(struct k_itimer * timer)168 static int posix_timer_add(struct k_itimer *timer)
169 {
170 	struct signal_struct *sig = current->signal;
171 	int first_free_id = sig->posix_timer_id;
172 	struct hlist_head *head;
173 	int ret = -ENOENT;
174 
175 	do {
176 		spin_lock(&hash_lock);
177 		head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
178 		if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
179 			hlist_add_head_rcu(&timer->t_hash, head);
180 			ret = sig->posix_timer_id;
181 		}
182 		if (++sig->posix_timer_id < 0)
183 			sig->posix_timer_id = 0;
184 		if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
185 			/* Loop over all possible ids completed */
186 			ret = -EAGAIN;
187 		spin_unlock(&hash_lock);
188 	} while (ret == -ENOENT);
189 	return ret;
190 }
191 
unlock_timer(struct k_itimer * timr,unsigned long flags)192 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
193 {
194 	spin_unlock_irqrestore(&timr->it_lock, flags);
195 }
196 
197 /* Get clock_realtime */
posix_clock_realtime_get(clockid_t which_clock,struct timespec64 * tp)198 static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
199 {
200 	ktime_get_real_ts64(tp);
201 	return 0;
202 }
203 
204 /* Set clock_realtime */
posix_clock_realtime_set(const clockid_t which_clock,const struct timespec64 * tp)205 static int posix_clock_realtime_set(const clockid_t which_clock,
206 				    const struct timespec64 *tp)
207 {
208 	return do_sys_settimeofday64(tp, NULL);
209 }
210 
posix_clock_realtime_adj(const clockid_t which_clock,struct timex * t)211 static int posix_clock_realtime_adj(const clockid_t which_clock,
212 				    struct timex *t)
213 {
214 	return do_adjtimex(t);
215 }
216 
217 /*
218  * Get monotonic time for posix timers
219  */
posix_ktime_get_ts(clockid_t which_clock,struct timespec64 * tp)220 static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
221 {
222 	ktime_get_ts64(tp);
223 	return 0;
224 }
225 
226 /*
227  * Get monotonic-raw time for posix timers
228  */
posix_get_monotonic_raw(clockid_t which_clock,struct timespec64 * tp)229 static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
230 {
231 	getrawmonotonic64(tp);
232 	return 0;
233 }
234 
235 
posix_get_realtime_coarse(clockid_t which_clock,struct timespec64 * tp)236 static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
237 {
238 	*tp = current_kernel_time64();
239 	return 0;
240 }
241 
posix_get_monotonic_coarse(clockid_t which_clock,struct timespec64 * tp)242 static int posix_get_monotonic_coarse(clockid_t which_clock,
243 						struct timespec64 *tp)
244 {
245 	*tp = get_monotonic_coarse64();
246 	return 0;
247 }
248 
posix_get_coarse_res(const clockid_t which_clock,struct timespec64 * tp)249 static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
250 {
251 	*tp = ktime_to_timespec64(KTIME_LOW_RES);
252 	return 0;
253 }
254 
posix_get_boottime(const clockid_t which_clock,struct timespec64 * tp)255 static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
256 {
257 	get_monotonic_boottime64(tp);
258 	return 0;
259 }
260 
posix_get_tai(clockid_t which_clock,struct timespec64 * tp)261 static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
262 {
263 	timekeeping_clocktai64(tp);
264 	return 0;
265 }
266 
posix_get_hrtimer_res(clockid_t which_clock,struct timespec64 * tp)267 static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
268 {
269 	tp->tv_sec = 0;
270 	tp->tv_nsec = hrtimer_resolution;
271 	return 0;
272 }
273 
274 /*
275  * Initialize everything, well, just everything in Posix clocks/timers ;)
276  */
init_posix_timers(void)277 static __init int init_posix_timers(void)
278 {
279 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
280 					sizeof (struct k_itimer), 0, SLAB_PANIC,
281 					NULL);
282 	return 0;
283 }
284 __initcall(init_posix_timers);
285 
286 /*
287  * The siginfo si_overrun field and the return value of timer_getoverrun(2)
288  * are of type int. Clamp the overrun value to INT_MAX
289  */
timer_overrun_to_int(struct k_itimer * timr,int baseval)290 static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
291 {
292 	s64 sum = timr->it_overrun_last + (s64)baseval;
293 
294 	return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
295 }
296 
common_hrtimer_rearm(struct k_itimer * timr)297 static void common_hrtimer_rearm(struct k_itimer *timr)
298 {
299 	struct hrtimer *timer = &timr->it.real.timer;
300 
301 	timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
302 					    timr->it_interval);
303 	hrtimer_restart(timer);
304 }
305 
306 /*
307  * This function is exported for use by the signal deliver code.  It is
308  * called just prior to the info block being released and passes that
309  * block to us.  It's function is to update the overrun entry AND to
310  * restart the timer.  It should only be called if the timer is to be
311  * restarted (i.e. we have flagged this in the sys_private entry of the
312  * info block).
313  *
314  * To protect against the timer going away while the interrupt is queued,
315  * we require that the it_requeue_pending flag be set.
316  */
posixtimer_rearm(struct siginfo * info)317 void posixtimer_rearm(struct siginfo *info)
318 {
319 	struct k_itimer *timr;
320 	unsigned long flags;
321 
322 	timr = lock_timer(info->si_tid, &flags);
323 	if (!timr)
324 		return;
325 
326 	if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
327 		timr->kclock->timer_rearm(timr);
328 
329 		timr->it_active = 1;
330 		timr->it_overrun_last = timr->it_overrun;
331 		timr->it_overrun = -1LL;
332 		++timr->it_requeue_pending;
333 
334 		info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
335 	}
336 
337 	unlock_timer(timr, flags);
338 }
339 
posix_timer_event(struct k_itimer * timr,int si_private)340 int posix_timer_event(struct k_itimer *timr, int si_private)
341 {
342 	struct task_struct *task;
343 	int shared, ret = -1;
344 	/*
345 	 * FIXME: if ->sigq is queued we can race with
346 	 * dequeue_signal()->posixtimer_rearm().
347 	 *
348 	 * If dequeue_signal() sees the "right" value of
349 	 * si_sys_private it calls posixtimer_rearm().
350 	 * We re-queue ->sigq and drop ->it_lock().
351 	 * posixtimer_rearm() locks the timer
352 	 * and re-schedules it while ->sigq is pending.
353 	 * Not really bad, but not that we want.
354 	 */
355 	timr->sigq->info.si_sys_private = si_private;
356 
357 	rcu_read_lock();
358 	task = pid_task(timr->it_pid, PIDTYPE_PID);
359 	if (task) {
360 		shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
361 		ret = send_sigqueue(timr->sigq, task, shared);
362 	}
363 	rcu_read_unlock();
364 	/* If we failed to send the signal the timer stops. */
365 	return ret > 0;
366 }
367 
368 /*
369  * This function gets called when a POSIX.1b interval timer expires.  It
370  * is used as a callback from the kernel internal timer.  The
371  * run_timer_list code ALWAYS calls with interrupts on.
372 
373  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
374  */
posix_timer_fn(struct hrtimer * timer)375 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
376 {
377 	struct k_itimer *timr;
378 	unsigned long flags;
379 	int si_private = 0;
380 	enum hrtimer_restart ret = HRTIMER_NORESTART;
381 
382 	timr = container_of(timer, struct k_itimer, it.real.timer);
383 	spin_lock_irqsave(&timr->it_lock, flags);
384 
385 	timr->it_active = 0;
386 	if (timr->it_interval != 0)
387 		si_private = ++timr->it_requeue_pending;
388 
389 	if (posix_timer_event(timr, si_private)) {
390 		/*
391 		 * signal was not sent because of sig_ignor
392 		 * we will not get a call back to restart it AND
393 		 * it should be restarted.
394 		 */
395 		if (timr->it_interval != 0) {
396 			ktime_t now = hrtimer_cb_get_time(timer);
397 
398 			/*
399 			 * FIXME: What we really want, is to stop this
400 			 * timer completely and restart it in case the
401 			 * SIG_IGN is removed. This is a non trivial
402 			 * change which involves sighand locking
403 			 * (sigh !), which we don't want to do late in
404 			 * the release cycle.
405 			 *
406 			 * For now we just let timers with an interval
407 			 * less than a jiffie expire every jiffie to
408 			 * avoid softirq starvation in case of SIG_IGN
409 			 * and a very small interval, which would put
410 			 * the timer right back on the softirq pending
411 			 * list. By moving now ahead of time we trick
412 			 * hrtimer_forward() to expire the timer
413 			 * later, while we still maintain the overrun
414 			 * accuracy, but have some inconsistency in
415 			 * the timer_gettime() case. This is at least
416 			 * better than a starved softirq. A more
417 			 * complex fix which solves also another related
418 			 * inconsistency is already in the pipeline.
419 			 */
420 #ifdef CONFIG_HIGH_RES_TIMERS
421 			{
422 				ktime_t kj = NSEC_PER_SEC / HZ;
423 
424 				if (timr->it_interval < kj)
425 					now = ktime_add(now, kj);
426 			}
427 #endif
428 			timr->it_overrun += hrtimer_forward(timer, now,
429 							    timr->it_interval);
430 			ret = HRTIMER_RESTART;
431 			++timr->it_requeue_pending;
432 			timr->it_active = 1;
433 		}
434 	}
435 
436 	unlock_timer(timr, flags);
437 	return ret;
438 }
439 
good_sigevent(sigevent_t * event)440 static struct pid *good_sigevent(sigevent_t * event)
441 {
442 	struct task_struct *rtn = current->group_leader;
443 
444 	switch (event->sigev_notify) {
445 	case SIGEV_SIGNAL | SIGEV_THREAD_ID:
446 		rtn = find_task_by_vpid(event->sigev_notify_thread_id);
447 		if (!rtn || !same_thread_group(rtn, current))
448 			return NULL;
449 		/* FALLTHRU */
450 	case SIGEV_SIGNAL:
451 	case SIGEV_THREAD:
452 		if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
453 			return NULL;
454 		/* FALLTHRU */
455 	case SIGEV_NONE:
456 		return task_pid(rtn);
457 	default:
458 		return NULL;
459 	}
460 }
461 
alloc_posix_timer(void)462 static struct k_itimer * alloc_posix_timer(void)
463 {
464 	struct k_itimer *tmr;
465 	tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
466 	if (!tmr)
467 		return tmr;
468 	if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
469 		kmem_cache_free(posix_timers_cache, tmr);
470 		return NULL;
471 	}
472 	memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
473 	return tmr;
474 }
475 
k_itimer_rcu_free(struct rcu_head * head)476 static void k_itimer_rcu_free(struct rcu_head *head)
477 {
478 	struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
479 
480 	kmem_cache_free(posix_timers_cache, tmr);
481 }
482 
483 #define IT_ID_SET	1
484 #define IT_ID_NOT_SET	0
release_posix_timer(struct k_itimer * tmr,int it_id_set)485 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
486 {
487 	if (it_id_set) {
488 		unsigned long flags;
489 		spin_lock_irqsave(&hash_lock, flags);
490 		hlist_del_rcu(&tmr->t_hash);
491 		spin_unlock_irqrestore(&hash_lock, flags);
492 	}
493 	put_pid(tmr->it_pid);
494 	sigqueue_free(tmr->sigq);
495 	call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
496 }
497 
common_timer_create(struct k_itimer * new_timer)498 static int common_timer_create(struct k_itimer *new_timer)
499 {
500 	hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
501 	return 0;
502 }
503 
504 /* Create a POSIX.1b interval timer. */
do_timer_create(clockid_t which_clock,struct sigevent * event,timer_t __user * created_timer_id)505 static int do_timer_create(clockid_t which_clock, struct sigevent *event,
506 			   timer_t __user *created_timer_id)
507 {
508 	const struct k_clock *kc = clockid_to_kclock(which_clock);
509 	struct k_itimer *new_timer;
510 	int error, new_timer_id;
511 	int it_id_set = IT_ID_NOT_SET;
512 
513 	if (!kc)
514 		return -EINVAL;
515 	if (!kc->timer_create)
516 		return -EOPNOTSUPP;
517 
518 	new_timer = alloc_posix_timer();
519 	if (unlikely(!new_timer))
520 		return -EAGAIN;
521 
522 	spin_lock_init(&new_timer->it_lock);
523 	new_timer_id = posix_timer_add(new_timer);
524 	if (new_timer_id < 0) {
525 		error = new_timer_id;
526 		goto out;
527 	}
528 
529 	it_id_set = IT_ID_SET;
530 	new_timer->it_id = (timer_t) new_timer_id;
531 	new_timer->it_clock = which_clock;
532 	new_timer->kclock = kc;
533 	new_timer->it_overrun = -1LL;
534 
535 	if (event) {
536 		rcu_read_lock();
537 		new_timer->it_pid = get_pid(good_sigevent(event));
538 		rcu_read_unlock();
539 		if (!new_timer->it_pid) {
540 			error = -EINVAL;
541 			goto out;
542 		}
543 		new_timer->it_sigev_notify     = event->sigev_notify;
544 		new_timer->sigq->info.si_signo = event->sigev_signo;
545 		new_timer->sigq->info.si_value = event->sigev_value;
546 	} else {
547 		new_timer->it_sigev_notify     = SIGEV_SIGNAL;
548 		new_timer->sigq->info.si_signo = SIGALRM;
549 		memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
550 		new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
551 		new_timer->it_pid = get_pid(task_tgid(current));
552 	}
553 
554 	new_timer->sigq->info.si_tid   = new_timer->it_id;
555 	new_timer->sigq->info.si_code  = SI_TIMER;
556 
557 	if (copy_to_user(created_timer_id,
558 			 &new_timer_id, sizeof (new_timer_id))) {
559 		error = -EFAULT;
560 		goto out;
561 	}
562 
563 	error = kc->timer_create(new_timer);
564 	if (error)
565 		goto out;
566 
567 	spin_lock_irq(&current->sighand->siglock);
568 	new_timer->it_signal = current->signal;
569 	list_add(&new_timer->list, &current->signal->posix_timers);
570 	spin_unlock_irq(&current->sighand->siglock);
571 
572 	return 0;
573 	/*
574 	 * In the case of the timer belonging to another task, after
575 	 * the task is unlocked, the timer is owned by the other task
576 	 * and may cease to exist at any time.  Don't use or modify
577 	 * new_timer after the unlock call.
578 	 */
579 out:
580 	release_posix_timer(new_timer, it_id_set);
581 	return error;
582 }
583 
SYSCALL_DEFINE3(timer_create,const clockid_t,which_clock,struct sigevent __user *,timer_event_spec,timer_t __user *,created_timer_id)584 SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
585 		struct sigevent __user *, timer_event_spec,
586 		timer_t __user *, created_timer_id)
587 {
588 	if (timer_event_spec) {
589 		sigevent_t event;
590 
591 		if (copy_from_user(&event, timer_event_spec, sizeof (event)))
592 			return -EFAULT;
593 		return do_timer_create(which_clock, &event, created_timer_id);
594 	}
595 	return do_timer_create(which_clock, NULL, created_timer_id);
596 }
597 
598 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE3(timer_create,clockid_t,which_clock,struct compat_sigevent __user *,timer_event_spec,timer_t __user *,created_timer_id)599 COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
600 		       struct compat_sigevent __user *, timer_event_spec,
601 		       timer_t __user *, created_timer_id)
602 {
603 	if (timer_event_spec) {
604 		sigevent_t event;
605 
606 		if (get_compat_sigevent(&event, timer_event_spec))
607 			return -EFAULT;
608 		return do_timer_create(which_clock, &event, created_timer_id);
609 	}
610 	return do_timer_create(which_clock, NULL, created_timer_id);
611 }
612 #endif
613 
614 /*
615  * Locking issues: We need to protect the result of the id look up until
616  * we get the timer locked down so it is not deleted under us.  The
617  * removal is done under the idr spinlock so we use that here to bridge
618  * the find to the timer lock.  To avoid a dead lock, the timer id MUST
619  * be release with out holding the timer lock.
620  */
__lock_timer(timer_t timer_id,unsigned long * flags)621 static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
622 {
623 	struct k_itimer *timr;
624 
625 	/*
626 	 * timer_t could be any type >= int and we want to make sure any
627 	 * @timer_id outside positive int range fails lookup.
628 	 */
629 	if ((unsigned long long)timer_id > INT_MAX)
630 		return NULL;
631 
632 	rcu_read_lock();
633 	timr = posix_timer_by_id(timer_id);
634 	if (timr) {
635 		spin_lock_irqsave(&timr->it_lock, *flags);
636 		if (timr->it_signal == current->signal) {
637 			rcu_read_unlock();
638 			return timr;
639 		}
640 		spin_unlock_irqrestore(&timr->it_lock, *flags);
641 	}
642 	rcu_read_unlock();
643 
644 	return NULL;
645 }
646 
common_hrtimer_remaining(struct k_itimer * timr,ktime_t now)647 static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
648 {
649 	struct hrtimer *timer = &timr->it.real.timer;
650 
651 	return __hrtimer_expires_remaining_adjusted(timer, now);
652 }
653 
common_hrtimer_forward(struct k_itimer * timr,ktime_t now)654 static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
655 {
656 	struct hrtimer *timer = &timr->it.real.timer;
657 
658 	return hrtimer_forward(timer, now, timr->it_interval);
659 }
660 
661 /*
662  * Get the time remaining on a POSIX.1b interval timer.  This function
663  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
664  * mess with irq.
665  *
666  * We have a couple of messes to clean up here.  First there is the case
667  * of a timer that has a requeue pending.  These timers should appear to
668  * be in the timer list with an expiry as if we were to requeue them
669  * now.
670  *
671  * The second issue is the SIGEV_NONE timer which may be active but is
672  * not really ever put in the timer list (to save system resources).
673  * This timer may be expired, and if so, we will do it here.  Otherwise
674  * it is the same as a requeue pending timer WRT to what we should
675  * report.
676  */
common_timer_get(struct k_itimer * timr,struct itimerspec64 * cur_setting)677 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
678 {
679 	const struct k_clock *kc = timr->kclock;
680 	ktime_t now, remaining, iv;
681 	struct timespec64 ts64;
682 	bool sig_none;
683 
684 	sig_none = timr->it_sigev_notify == SIGEV_NONE;
685 	iv = timr->it_interval;
686 
687 	/* interval timer ? */
688 	if (iv) {
689 		cur_setting->it_interval = ktime_to_timespec64(iv);
690 	} else if (!timr->it_active) {
691 		/*
692 		 * SIGEV_NONE oneshot timers are never queued. Check them
693 		 * below.
694 		 */
695 		if (!sig_none)
696 			return;
697 	}
698 
699 	/*
700 	 * The timespec64 based conversion is suboptimal, but it's not
701 	 * worth to implement yet another callback.
702 	 */
703 	kc->clock_get(timr->it_clock, &ts64);
704 	now = timespec64_to_ktime(ts64);
705 
706 	/*
707 	 * When a requeue is pending or this is a SIGEV_NONE timer move the
708 	 * expiry time forward by intervals, so expiry is > now.
709 	 */
710 	if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
711 		timr->it_overrun += kc->timer_forward(timr, now);
712 
713 	remaining = kc->timer_remaining(timr, now);
714 	/* Return 0 only, when the timer is expired and not pending */
715 	if (remaining <= 0) {
716 		/*
717 		 * A single shot SIGEV_NONE timer must return 0, when
718 		 * it is expired !
719 		 */
720 		if (!sig_none)
721 			cur_setting->it_value.tv_nsec = 1;
722 	} else {
723 		cur_setting->it_value = ktime_to_timespec64(remaining);
724 	}
725 }
726 
727 /* Get the time remaining on a POSIX.1b interval timer. */
do_timer_gettime(timer_t timer_id,struct itimerspec64 * setting)728 static int do_timer_gettime(timer_t timer_id,  struct itimerspec64 *setting)
729 {
730 	struct k_itimer *timr;
731 	const struct k_clock *kc;
732 	unsigned long flags;
733 	int ret = 0;
734 
735 	timr = lock_timer(timer_id, &flags);
736 	if (!timr)
737 		return -EINVAL;
738 
739 	memset(setting, 0, sizeof(*setting));
740 	kc = timr->kclock;
741 	if (WARN_ON_ONCE(!kc || !kc->timer_get))
742 		ret = -EINVAL;
743 	else
744 		kc->timer_get(timr, setting);
745 
746 	unlock_timer(timr, flags);
747 	return ret;
748 }
749 
750 /* Get the time remaining on a POSIX.1b interval timer. */
SYSCALL_DEFINE2(timer_gettime,timer_t,timer_id,struct itimerspec __user *,setting)751 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
752 		struct itimerspec __user *, setting)
753 {
754 	struct itimerspec64 cur_setting;
755 
756 	int ret = do_timer_gettime(timer_id, &cur_setting);
757 	if (!ret) {
758 		if (put_itimerspec64(&cur_setting, setting))
759 			ret = -EFAULT;
760 	}
761 	return ret;
762 }
763 
764 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(timer_gettime,timer_t,timer_id,struct compat_itimerspec __user *,setting)765 COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
766 		       struct compat_itimerspec __user *, setting)
767 {
768 	struct itimerspec64 cur_setting;
769 
770 	int ret = do_timer_gettime(timer_id, &cur_setting);
771 	if (!ret) {
772 		if (put_compat_itimerspec64(&cur_setting, setting))
773 			ret = -EFAULT;
774 	}
775 	return ret;
776 }
777 #endif
778 
779 /*
780  * Get the number of overruns of a POSIX.1b interval timer.  This is to
781  * be the overrun of the timer last delivered.  At the same time we are
782  * accumulating overruns on the next timer.  The overrun is frozen when
783  * the signal is delivered, either at the notify time (if the info block
784  * is not queued) or at the actual delivery time (as we are informed by
785  * the call back to posixtimer_rearm().  So all we need to do is
786  * to pick up the frozen overrun.
787  */
SYSCALL_DEFINE1(timer_getoverrun,timer_t,timer_id)788 SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
789 {
790 	struct k_itimer *timr;
791 	int overrun;
792 	unsigned long flags;
793 
794 	timr = lock_timer(timer_id, &flags);
795 	if (!timr)
796 		return -EINVAL;
797 
798 	overrun = timer_overrun_to_int(timr, 0);
799 	unlock_timer(timr, flags);
800 
801 	return overrun;
802 }
803 
common_hrtimer_arm(struct k_itimer * timr,ktime_t expires,bool absolute,bool sigev_none)804 static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
805 			       bool absolute, bool sigev_none)
806 {
807 	struct hrtimer *timer = &timr->it.real.timer;
808 	enum hrtimer_mode mode;
809 
810 	mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
811 	/*
812 	 * Posix magic: Relative CLOCK_REALTIME timers are not affected by
813 	 * clock modifications, so they become CLOCK_MONOTONIC based under the
814 	 * hood. See hrtimer_init(). Update timr->kclock, so the generic
815 	 * functions which use timr->kclock->clock_get() work.
816 	 *
817 	 * Note: it_clock stays unmodified, because the next timer_set() might
818 	 * use ABSTIME, so it needs to switch back.
819 	 */
820 	if (timr->it_clock == CLOCK_REALTIME)
821 		timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
822 
823 	hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
824 	timr->it.real.timer.function = posix_timer_fn;
825 
826 	if (!absolute)
827 		expires = ktime_add_safe(expires, timer->base->get_time());
828 	hrtimer_set_expires(timer, expires);
829 
830 	if (!sigev_none)
831 		hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
832 }
833 
common_hrtimer_try_to_cancel(struct k_itimer * timr)834 static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
835 {
836 	return hrtimer_try_to_cancel(&timr->it.real.timer);
837 }
838 
839 /* Set a POSIX.1b interval timer. */
common_timer_set(struct k_itimer * timr,int flags,struct itimerspec64 * new_setting,struct itimerspec64 * old_setting)840 int common_timer_set(struct k_itimer *timr, int flags,
841 		     struct itimerspec64 *new_setting,
842 		     struct itimerspec64 *old_setting)
843 {
844 	const struct k_clock *kc = timr->kclock;
845 	bool sigev_none;
846 	ktime_t expires;
847 
848 	if (old_setting)
849 		common_timer_get(timr, old_setting);
850 
851 	/* Prevent rearming by clearing the interval */
852 	timr->it_interval = 0;
853 	/*
854 	 * Careful here. On SMP systems the timer expiry function could be
855 	 * active and spinning on timr->it_lock.
856 	 */
857 	if (kc->timer_try_to_cancel(timr) < 0)
858 		return TIMER_RETRY;
859 
860 	timr->it_active = 0;
861 	timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
862 		~REQUEUE_PENDING;
863 	timr->it_overrun_last = 0;
864 
865 	/* Switch off the timer when it_value is zero */
866 	if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
867 		return 0;
868 
869 	timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
870 	expires = timespec64_to_ktime(new_setting->it_value);
871 	sigev_none = timr->it_sigev_notify == SIGEV_NONE;
872 
873 	kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
874 	timr->it_active = !sigev_none;
875 	return 0;
876 }
877 
do_timer_settime(timer_t timer_id,int flags,struct itimerspec64 * new_spec64,struct itimerspec64 * old_spec64)878 static int do_timer_settime(timer_t timer_id, int flags,
879 			    struct itimerspec64 *new_spec64,
880 			    struct itimerspec64 *old_spec64)
881 {
882 	const struct k_clock *kc;
883 	struct k_itimer *timr;
884 	unsigned long flag;
885 	int error = 0;
886 
887 	if (!timespec64_valid(&new_spec64->it_interval) ||
888 	    !timespec64_valid(&new_spec64->it_value))
889 		return -EINVAL;
890 
891 	if (old_spec64)
892 		memset(old_spec64, 0, sizeof(*old_spec64));
893 retry:
894 	timr = lock_timer(timer_id, &flag);
895 	if (!timr)
896 		return -EINVAL;
897 
898 	kc = timr->kclock;
899 	if (WARN_ON_ONCE(!kc || !kc->timer_set))
900 		error = -EINVAL;
901 	else
902 		error = kc->timer_set(timr, flags, new_spec64, old_spec64);
903 
904 	unlock_timer(timr, flag);
905 	if (error == TIMER_RETRY) {
906 		old_spec64 = NULL;	// We already got the old time...
907 		goto retry;
908 	}
909 
910 	return error;
911 }
912 
913 /* Set a POSIX.1b interval timer */
SYSCALL_DEFINE4(timer_settime,timer_t,timer_id,int,flags,const struct itimerspec __user *,new_setting,struct itimerspec __user *,old_setting)914 SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
915 		const struct itimerspec __user *, new_setting,
916 		struct itimerspec __user *, old_setting)
917 {
918 	struct itimerspec64 new_spec, old_spec;
919 	struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
920 	int error = 0;
921 
922 	if (!new_setting)
923 		return -EINVAL;
924 
925 	if (get_itimerspec64(&new_spec, new_setting))
926 		return -EFAULT;
927 
928 	error = do_timer_settime(timer_id, flags, &new_spec, rtn);
929 	if (!error && old_setting) {
930 		if (put_itimerspec64(&old_spec, old_setting))
931 			error = -EFAULT;
932 	}
933 	return error;
934 }
935 
936 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(timer_settime,timer_t,timer_id,int,flags,struct compat_itimerspec __user *,new,struct compat_itimerspec __user *,old)937 COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
938 		       struct compat_itimerspec __user *, new,
939 		       struct compat_itimerspec __user *, old)
940 {
941 	struct itimerspec64 new_spec, old_spec;
942 	struct itimerspec64 *rtn = old ? &old_spec : NULL;
943 	int error = 0;
944 
945 	if (!new)
946 		return -EINVAL;
947 	if (get_compat_itimerspec64(&new_spec, new))
948 		return -EFAULT;
949 
950 	error = do_timer_settime(timer_id, flags, &new_spec, rtn);
951 	if (!error && old) {
952 		if (put_compat_itimerspec64(&old_spec, old))
953 			error = -EFAULT;
954 	}
955 	return error;
956 }
957 #endif
958 
common_timer_del(struct k_itimer * timer)959 int common_timer_del(struct k_itimer *timer)
960 {
961 	const struct k_clock *kc = timer->kclock;
962 
963 	timer->it_interval = 0;
964 	if (kc->timer_try_to_cancel(timer) < 0)
965 		return TIMER_RETRY;
966 	timer->it_active = 0;
967 	return 0;
968 }
969 
timer_delete_hook(struct k_itimer * timer)970 static inline int timer_delete_hook(struct k_itimer *timer)
971 {
972 	const struct k_clock *kc = timer->kclock;
973 
974 	if (WARN_ON_ONCE(!kc || !kc->timer_del))
975 		return -EINVAL;
976 	return kc->timer_del(timer);
977 }
978 
979 /* Delete a POSIX.1b interval timer. */
SYSCALL_DEFINE1(timer_delete,timer_t,timer_id)980 SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
981 {
982 	struct k_itimer *timer;
983 	unsigned long flags;
984 
985 retry_delete:
986 	timer = lock_timer(timer_id, &flags);
987 	if (!timer)
988 		return -EINVAL;
989 
990 	if (timer_delete_hook(timer) == TIMER_RETRY) {
991 		unlock_timer(timer, flags);
992 		goto retry_delete;
993 	}
994 
995 	spin_lock(&current->sighand->siglock);
996 	list_del(&timer->list);
997 	spin_unlock(&current->sighand->siglock);
998 	/*
999 	 * This keeps any tasks waiting on the spin lock from thinking
1000 	 * they got something (see the lock code above).
1001 	 */
1002 	timer->it_signal = NULL;
1003 
1004 	unlock_timer(timer, flags);
1005 	release_posix_timer(timer, IT_ID_SET);
1006 	return 0;
1007 }
1008 
1009 /*
1010  * return timer owned by the process, used by exit_itimers
1011  */
itimer_delete(struct k_itimer * timer)1012 static void itimer_delete(struct k_itimer *timer)
1013 {
1014 	unsigned long flags;
1015 
1016 retry_delete:
1017 	spin_lock_irqsave(&timer->it_lock, flags);
1018 
1019 	if (timer_delete_hook(timer) == TIMER_RETRY) {
1020 		unlock_timer(timer, flags);
1021 		goto retry_delete;
1022 	}
1023 	list_del(&timer->list);
1024 	/*
1025 	 * This keeps any tasks waiting on the spin lock from thinking
1026 	 * they got something (see the lock code above).
1027 	 */
1028 	timer->it_signal = NULL;
1029 
1030 	unlock_timer(timer, flags);
1031 	release_posix_timer(timer, IT_ID_SET);
1032 }
1033 
1034 /*
1035  * This is called by do_exit or de_thread, only when there are no more
1036  * references to the shared signal_struct.
1037  */
exit_itimers(struct signal_struct * sig)1038 void exit_itimers(struct signal_struct *sig)
1039 {
1040 	struct k_itimer *tmr;
1041 
1042 	while (!list_empty(&sig->posix_timers)) {
1043 		tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1044 		itimer_delete(tmr);
1045 	}
1046 }
1047 
SYSCALL_DEFINE2(clock_settime,const clockid_t,which_clock,const struct timespec __user *,tp)1048 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
1049 		const struct timespec __user *, tp)
1050 {
1051 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1052 	struct timespec64 new_tp;
1053 
1054 	if (!kc || !kc->clock_set)
1055 		return -EINVAL;
1056 
1057 	if (get_timespec64(&new_tp, tp))
1058 		return -EFAULT;
1059 
1060 	return kc->clock_set(which_clock, &new_tp);
1061 }
1062 
SYSCALL_DEFINE2(clock_gettime,const clockid_t,which_clock,struct timespec __user *,tp)1063 SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
1064 		struct timespec __user *,tp)
1065 {
1066 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1067 	struct timespec64 kernel_tp;
1068 	int error;
1069 
1070 	if (!kc)
1071 		return -EINVAL;
1072 
1073 	error = kc->clock_get(which_clock, &kernel_tp);
1074 
1075 	if (!error && put_timespec64(&kernel_tp, tp))
1076 		error = -EFAULT;
1077 
1078 	return error;
1079 }
1080 
SYSCALL_DEFINE2(clock_adjtime,const clockid_t,which_clock,struct timex __user *,utx)1081 SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
1082 		struct timex __user *, utx)
1083 {
1084 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1085 	struct timex ktx;
1086 	int err;
1087 
1088 	if (!kc)
1089 		return -EINVAL;
1090 	if (!kc->clock_adj)
1091 		return -EOPNOTSUPP;
1092 
1093 	if (copy_from_user(&ktx, utx, sizeof(ktx)))
1094 		return -EFAULT;
1095 
1096 	err = kc->clock_adj(which_clock, &ktx);
1097 
1098 	if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
1099 		return -EFAULT;
1100 
1101 	return err;
1102 }
1103 
SYSCALL_DEFINE2(clock_getres,const clockid_t,which_clock,struct timespec __user *,tp)1104 SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1105 		struct timespec __user *, tp)
1106 {
1107 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1108 	struct timespec64 rtn_tp;
1109 	int error;
1110 
1111 	if (!kc)
1112 		return -EINVAL;
1113 
1114 	error = kc->clock_getres(which_clock, &rtn_tp);
1115 
1116 	if (!error && tp && put_timespec64(&rtn_tp, tp))
1117 		error = -EFAULT;
1118 
1119 	return error;
1120 }
1121 
1122 #ifdef CONFIG_COMPAT
1123 
COMPAT_SYSCALL_DEFINE2(clock_settime,clockid_t,which_clock,struct compat_timespec __user *,tp)1124 COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,
1125 		       struct compat_timespec __user *, tp)
1126 {
1127 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1128 	struct timespec64 ts;
1129 
1130 	if (!kc || !kc->clock_set)
1131 		return -EINVAL;
1132 
1133 	if (compat_get_timespec64(&ts, tp))
1134 		return -EFAULT;
1135 
1136 	return kc->clock_set(which_clock, &ts);
1137 }
1138 
COMPAT_SYSCALL_DEFINE2(clock_gettime,clockid_t,which_clock,struct compat_timespec __user *,tp)1139 COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock,
1140 		       struct compat_timespec __user *, tp)
1141 {
1142 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1143 	struct timespec64 ts;
1144 	int err;
1145 
1146 	if (!kc)
1147 		return -EINVAL;
1148 
1149 	err = kc->clock_get(which_clock, &ts);
1150 
1151 	if (!err && compat_put_timespec64(&ts, tp))
1152 		err = -EFAULT;
1153 
1154 	return err;
1155 }
1156 
COMPAT_SYSCALL_DEFINE2(clock_adjtime,clockid_t,which_clock,struct compat_timex __user *,utp)1157 COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
1158 		       struct compat_timex __user *, utp)
1159 {
1160 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1161 	struct timex ktx;
1162 	int err;
1163 
1164 	if (!kc)
1165 		return -EINVAL;
1166 	if (!kc->clock_adj)
1167 		return -EOPNOTSUPP;
1168 
1169 	err = compat_get_timex(&ktx, utp);
1170 	if (err)
1171 		return err;
1172 
1173 	err = kc->clock_adj(which_clock, &ktx);
1174 
1175 	if (err >= 0)
1176 		err = compat_put_timex(utp, &ktx);
1177 
1178 	return err;
1179 }
1180 
COMPAT_SYSCALL_DEFINE2(clock_getres,clockid_t,which_clock,struct compat_timespec __user *,tp)1181 COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
1182 		       struct compat_timespec __user *, tp)
1183 {
1184 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1185 	struct timespec64 ts;
1186 	int err;
1187 
1188 	if (!kc)
1189 		return -EINVAL;
1190 
1191 	err = kc->clock_getres(which_clock, &ts);
1192 	if (!err && tp && compat_put_timespec64(&ts, tp))
1193 		return -EFAULT;
1194 
1195 	return err;
1196 }
1197 
1198 #endif
1199 
1200 /*
1201  * nanosleep for monotonic and realtime clocks
1202  */
common_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * rqtp)1203 static int common_nsleep(const clockid_t which_clock, int flags,
1204 			 const struct timespec64 *rqtp)
1205 {
1206 	return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
1207 				 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1208 				 which_clock);
1209 }
1210 
SYSCALL_DEFINE4(clock_nanosleep,const clockid_t,which_clock,int,flags,const struct timespec __user *,rqtp,struct timespec __user *,rmtp)1211 SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1212 		const struct timespec __user *, rqtp,
1213 		struct timespec __user *, rmtp)
1214 {
1215 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1216 	struct timespec64 t;
1217 
1218 	if (!kc)
1219 		return -EINVAL;
1220 	if (!kc->nsleep)
1221 		return -ENANOSLEEP_NOTSUP;
1222 
1223 	if (get_timespec64(&t, rqtp))
1224 		return -EFAULT;
1225 
1226 	if (!timespec64_valid(&t))
1227 		return -EINVAL;
1228 	if (flags & TIMER_ABSTIME)
1229 		rmtp = NULL;
1230 	current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
1231 	current->restart_block.nanosleep.rmtp = rmtp;
1232 
1233 	return kc->nsleep(which_clock, flags, &t);
1234 }
1235 
1236 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(clock_nanosleep,clockid_t,which_clock,int,flags,struct compat_timespec __user *,rqtp,struct compat_timespec __user *,rmtp)1237 COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
1238 		       struct compat_timespec __user *, rqtp,
1239 		       struct compat_timespec __user *, rmtp)
1240 {
1241 	const struct k_clock *kc = clockid_to_kclock(which_clock);
1242 	struct timespec64 t;
1243 
1244 	if (!kc)
1245 		return -EINVAL;
1246 	if (!kc->nsleep)
1247 		return -ENANOSLEEP_NOTSUP;
1248 
1249 	if (compat_get_timespec64(&t, rqtp))
1250 		return -EFAULT;
1251 
1252 	if (!timespec64_valid(&t))
1253 		return -EINVAL;
1254 	if (flags & TIMER_ABSTIME)
1255 		rmtp = NULL;
1256 	current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1257 	current->restart_block.nanosleep.compat_rmtp = rmtp;
1258 
1259 	return kc->nsleep(which_clock, flags, &t);
1260 }
1261 #endif
1262 
1263 static const struct k_clock clock_realtime = {
1264 	.clock_getres		= posix_get_hrtimer_res,
1265 	.clock_get		= posix_clock_realtime_get,
1266 	.clock_set		= posix_clock_realtime_set,
1267 	.clock_adj		= posix_clock_realtime_adj,
1268 	.nsleep			= common_nsleep,
1269 	.timer_create		= common_timer_create,
1270 	.timer_set		= common_timer_set,
1271 	.timer_get		= common_timer_get,
1272 	.timer_del		= common_timer_del,
1273 	.timer_rearm		= common_hrtimer_rearm,
1274 	.timer_forward		= common_hrtimer_forward,
1275 	.timer_remaining	= common_hrtimer_remaining,
1276 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1277 	.timer_arm		= common_hrtimer_arm,
1278 };
1279 
1280 static const struct k_clock clock_monotonic = {
1281 	.clock_getres		= posix_get_hrtimer_res,
1282 	.clock_get		= posix_ktime_get_ts,
1283 	.nsleep			= common_nsleep,
1284 	.timer_create		= common_timer_create,
1285 	.timer_set		= common_timer_set,
1286 	.timer_get		= common_timer_get,
1287 	.timer_del		= common_timer_del,
1288 	.timer_rearm		= common_hrtimer_rearm,
1289 	.timer_forward		= common_hrtimer_forward,
1290 	.timer_remaining	= common_hrtimer_remaining,
1291 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1292 	.timer_arm		= common_hrtimer_arm,
1293 };
1294 
1295 static const struct k_clock clock_monotonic_raw = {
1296 	.clock_getres		= posix_get_hrtimer_res,
1297 	.clock_get		= posix_get_monotonic_raw,
1298 };
1299 
1300 static const struct k_clock clock_realtime_coarse = {
1301 	.clock_getres		= posix_get_coarse_res,
1302 	.clock_get		= posix_get_realtime_coarse,
1303 };
1304 
1305 static const struct k_clock clock_monotonic_coarse = {
1306 	.clock_getres		= posix_get_coarse_res,
1307 	.clock_get		= posix_get_monotonic_coarse,
1308 };
1309 
1310 static const struct k_clock clock_tai = {
1311 	.clock_getres		= posix_get_hrtimer_res,
1312 	.clock_get		= posix_get_tai,
1313 	.nsleep			= common_nsleep,
1314 	.timer_create		= common_timer_create,
1315 	.timer_set		= common_timer_set,
1316 	.timer_get		= common_timer_get,
1317 	.timer_del		= common_timer_del,
1318 	.timer_rearm		= common_hrtimer_rearm,
1319 	.timer_forward		= common_hrtimer_forward,
1320 	.timer_remaining	= common_hrtimer_remaining,
1321 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1322 	.timer_arm		= common_hrtimer_arm,
1323 };
1324 
1325 static const struct k_clock clock_boottime = {
1326 	.clock_getres		= posix_get_hrtimer_res,
1327 	.clock_get		= posix_get_boottime,
1328 	.nsleep			= common_nsleep,
1329 	.timer_create		= common_timer_create,
1330 	.timer_set		= common_timer_set,
1331 	.timer_get		= common_timer_get,
1332 	.timer_del		= common_timer_del,
1333 	.timer_rearm		= common_hrtimer_rearm,
1334 	.timer_forward		= common_hrtimer_forward,
1335 	.timer_remaining	= common_hrtimer_remaining,
1336 	.timer_try_to_cancel	= common_hrtimer_try_to_cancel,
1337 	.timer_arm		= common_hrtimer_arm,
1338 };
1339 
1340 static const struct k_clock * const posix_clocks[] = {
1341 	[CLOCK_REALTIME]		= &clock_realtime,
1342 	[CLOCK_MONOTONIC]		= &clock_monotonic,
1343 	[CLOCK_PROCESS_CPUTIME_ID]	= &clock_process,
1344 	[CLOCK_THREAD_CPUTIME_ID]	= &clock_thread,
1345 	[CLOCK_MONOTONIC_RAW]		= &clock_monotonic_raw,
1346 	[CLOCK_REALTIME_COARSE]		= &clock_realtime_coarse,
1347 	[CLOCK_MONOTONIC_COARSE]	= &clock_monotonic_coarse,
1348 	[CLOCK_BOOTTIME]		= &clock_boottime,
1349 	[CLOCK_REALTIME_ALARM]		= &alarm_clock,
1350 	[CLOCK_BOOTTIME_ALARM]		= &alarm_clock,
1351 	[CLOCK_TAI]			= &clock_tai,
1352 };
1353 
clockid_to_kclock(const clockid_t id)1354 static const struct k_clock *clockid_to_kclock(const clockid_t id)
1355 {
1356 	clockid_t idx = id;
1357 
1358 	if (id < 0) {
1359 		return (id & CLOCKFD_MASK) == CLOCKFD ?
1360 			&clock_posix_dynamic : &clock_posix_cpu;
1361 	}
1362 
1363 	if (id >= ARRAY_SIZE(posix_clocks))
1364 		return NULL;
1365 
1366 	return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
1367 }
1368