posix-timers.c 37 KB

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