Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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