posix-timers.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 2002-10-15 Posix Clocks & timers
  4. * by George Anzinger [email protected]
  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,
  242. SLAB_PANIC | SLAB_ACCOUNT, 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;
  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. /*
  735. * kc->timer_wait_running() might drop RCU lock. So @timer
  736. * cannot be touched anymore after the function returns!
  737. */
  738. if (!WARN_ON_ONCE(!kc->timer_wait_running))
  739. kc->timer_wait_running(timer);
  740. rcu_read_unlock();
  741. /* Relock the timer. It might be not longer hashed. */
  742. return lock_timer(timer_id, flags);
  743. }
  744. /* Set a POSIX.1b interval timer. */
  745. int common_timer_set(struct k_itimer *timr, int flags,
  746. struct itimerspec64 *new_setting,
  747. struct itimerspec64 *old_setting)
  748. {
  749. const struct k_clock *kc = timr->kclock;
  750. bool sigev_none;
  751. ktime_t expires;
  752. if (old_setting)
  753. common_timer_get(timr, old_setting);
  754. /* Prevent rearming by clearing the interval */
  755. timr->it_interval = 0;
  756. /*
  757. * Careful here. On SMP systems the timer expiry function could be
  758. * active and spinning on timr->it_lock.
  759. */
  760. if (kc->timer_try_to_cancel(timr) < 0)
  761. return TIMER_RETRY;
  762. timr->it_active = 0;
  763. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  764. ~REQUEUE_PENDING;
  765. timr->it_overrun_last = 0;
  766. /* Switch off the timer when it_value is zero */
  767. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  768. return 0;
  769. timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
  770. expires = timespec64_to_ktime(new_setting->it_value);
  771. if (flags & TIMER_ABSTIME)
  772. expires = timens_ktime_to_host(timr->it_clock, expires);
  773. sigev_none = timr->it_sigev_notify == SIGEV_NONE;
  774. kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
  775. timr->it_active = !sigev_none;
  776. return 0;
  777. }
  778. static int do_timer_settime(timer_t timer_id, int tmr_flags,
  779. struct itimerspec64 *new_spec64,
  780. struct itimerspec64 *old_spec64)
  781. {
  782. const struct k_clock *kc;
  783. struct k_itimer *timr;
  784. unsigned long flags;
  785. int error = 0;
  786. if (!timespec64_valid(&new_spec64->it_interval) ||
  787. !timespec64_valid(&new_spec64->it_value))
  788. return -EINVAL;
  789. if (old_spec64)
  790. memset(old_spec64, 0, sizeof(*old_spec64));
  791. timr = lock_timer(timer_id, &flags);
  792. retry:
  793. if (!timr)
  794. return -EINVAL;
  795. kc = timr->kclock;
  796. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  797. error = -EINVAL;
  798. else
  799. error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
  800. if (error == TIMER_RETRY) {
  801. // We already got the old time...
  802. old_spec64 = NULL;
  803. /* Unlocks and relocks the timer if it still exists */
  804. timr = timer_wait_running(timr, &flags);
  805. goto retry;
  806. }
  807. unlock_timer(timr, flags);
  808. return error;
  809. }
  810. /* Set a POSIX.1b interval timer */
  811. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  812. const struct __kernel_itimerspec __user *, new_setting,
  813. struct __kernel_itimerspec __user *, old_setting)
  814. {
  815. struct itimerspec64 new_spec, old_spec;
  816. struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
  817. int error = 0;
  818. if (!new_setting)
  819. return -EINVAL;
  820. if (get_itimerspec64(&new_spec, new_setting))
  821. return -EFAULT;
  822. error = do_timer_settime(timer_id, flags, &new_spec, rtn);
  823. if (!error && old_setting) {
  824. if (put_itimerspec64(&old_spec, old_setting))
  825. error = -EFAULT;
  826. }
  827. return error;
  828. }
  829. #ifdef CONFIG_COMPAT_32BIT_TIME
  830. SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
  831. struct old_itimerspec32 __user *, new,
  832. struct old_itimerspec32 __user *, old)
  833. {
  834. struct itimerspec64 new_spec, old_spec;
  835. struct itimerspec64 *rtn = old ? &old_spec : NULL;
  836. int error = 0;
  837. if (!new)
  838. return -EINVAL;
  839. if (get_old_itimerspec32(&new_spec, new))
  840. return -EFAULT;
  841. error = do_timer_settime(timer_id, flags, &new_spec, rtn);
  842. if (!error && old) {
  843. if (put_old_itimerspec32(&old_spec, old))
  844. error = -EFAULT;
  845. }
  846. return error;
  847. }
  848. #endif
  849. int common_timer_del(struct k_itimer *timer)
  850. {
  851. const struct k_clock *kc = timer->kclock;
  852. timer->it_interval = 0;
  853. if (kc->timer_try_to_cancel(timer) < 0)
  854. return TIMER_RETRY;
  855. timer->it_active = 0;
  856. return 0;
  857. }
  858. static inline int timer_delete_hook(struct k_itimer *timer)
  859. {
  860. const struct k_clock *kc = timer->kclock;
  861. if (WARN_ON_ONCE(!kc || !kc->timer_del))
  862. return -EINVAL;
  863. return kc->timer_del(timer);
  864. }
  865. /* Delete a POSIX.1b interval timer. */
  866. SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  867. {
  868. struct k_itimer *timer;
  869. unsigned long flags;
  870. timer = lock_timer(timer_id, &flags);
  871. retry_delete:
  872. if (!timer)
  873. return -EINVAL;
  874. if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
  875. /* Unlocks and relocks the timer if it still exists */
  876. timer = timer_wait_running(timer, &flags);
  877. goto retry_delete;
  878. }
  879. spin_lock(&current->sighand->siglock);
  880. list_del(&timer->list);
  881. spin_unlock(&current->sighand->siglock);
  882. /*
  883. * This keeps any tasks waiting on the spin lock from thinking
  884. * they got something (see the lock code above).
  885. */
  886. timer->it_signal = NULL;
  887. unlock_timer(timer, flags);
  888. release_posix_timer(timer, IT_ID_SET);
  889. return 0;
  890. }
  891. /*
  892. * Delete a timer if it is armed, remove it from the hash and schedule it
  893. * for RCU freeing.
  894. */
  895. static void itimer_delete(struct k_itimer *timer)
  896. {
  897. unsigned long flags;
  898. /*
  899. * irqsave is required to make timer_wait_running() work.
  900. */
  901. spin_lock_irqsave(&timer->it_lock, flags);
  902. retry_delete:
  903. /*
  904. * Even if the timer is not longer accessible from other tasks
  905. * it still might be armed and queued in the underlying timer
  906. * mechanism. Worse, that timer mechanism might run the expiry
  907. * function concurrently.
  908. */
  909. if (timer_delete_hook(timer) == TIMER_RETRY) {
  910. /*
  911. * Timer is expired concurrently, prevent livelocks
  912. * and pointless spinning on RT.
  913. *
  914. * timer_wait_running() drops timer::it_lock, which opens
  915. * the possibility for another task to delete the timer.
  916. *
  917. * That's not possible here because this is invoked from
  918. * do_exit() only for the last thread of the thread group.
  919. * So no other task can access and delete that timer.
  920. */
  921. if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
  922. return;
  923. goto retry_delete;
  924. }
  925. list_del(&timer->list);
  926. spin_unlock_irqrestore(&timer->it_lock, flags);
  927. release_posix_timer(timer, IT_ID_SET);
  928. }
  929. /*
  930. * Invoked from do_exit() when the last thread of a thread group exits.
  931. * At that point no other task can access the timers of the dying
  932. * task anymore.
  933. */
  934. void exit_itimers(struct task_struct *tsk)
  935. {
  936. struct list_head timers;
  937. struct k_itimer *tmr;
  938. if (list_empty(&tsk->signal->posix_timers))
  939. return;
  940. /* Protect against concurrent read via /proc/$PID/timers */
  941. spin_lock_irq(&tsk->sighand->siglock);
  942. list_replace_init(&tsk->signal->posix_timers, &timers);
  943. spin_unlock_irq(&tsk->sighand->siglock);
  944. /* The timers are not longer accessible via tsk::signal */
  945. while (!list_empty(&timers)) {
  946. tmr = list_first_entry(&timers, struct k_itimer, list);
  947. itimer_delete(tmr);
  948. }
  949. }
  950. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  951. const struct __kernel_timespec __user *, tp)
  952. {
  953. const struct k_clock *kc = clockid_to_kclock(which_clock);
  954. struct timespec64 new_tp;
  955. if (!kc || !kc->clock_set)
  956. return -EINVAL;
  957. if (get_timespec64(&new_tp, tp))
  958. return -EFAULT;
  959. return kc->clock_set(which_clock, &new_tp);
  960. }
  961. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  962. struct __kernel_timespec __user *, tp)
  963. {
  964. const struct k_clock *kc = clockid_to_kclock(which_clock);
  965. struct timespec64 kernel_tp;
  966. int error;
  967. if (!kc)
  968. return -EINVAL;
  969. error = kc->clock_get_timespec(which_clock, &kernel_tp);
  970. if (!error && put_timespec64(&kernel_tp, tp))
  971. error = -EFAULT;
  972. return error;
  973. }
  974. int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
  975. {
  976. const struct k_clock *kc = clockid_to_kclock(which_clock);
  977. if (!kc)
  978. return -EINVAL;
  979. if (!kc->clock_adj)
  980. return -EOPNOTSUPP;
  981. return kc->clock_adj(which_clock, ktx);
  982. }
  983. SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
  984. struct __kernel_timex __user *, utx)
  985. {
  986. struct __kernel_timex ktx;
  987. int err;
  988. if (copy_from_user(&ktx, utx, sizeof(ktx)))
  989. return -EFAULT;
  990. err = do_clock_adjtime(which_clock, &ktx);
  991. if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
  992. return -EFAULT;
  993. return err;
  994. }
  995. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
  996. struct __kernel_timespec __user *, tp)
  997. {
  998. const struct k_clock *kc = clockid_to_kclock(which_clock);
  999. struct timespec64 rtn_tp;
  1000. int error;
  1001. if (!kc)
  1002. return -EINVAL;
  1003. error = kc->clock_getres(which_clock, &rtn_tp);
  1004. if (!error && tp && put_timespec64(&rtn_tp, tp))
  1005. error = -EFAULT;
  1006. return error;
  1007. }
  1008. #ifdef CONFIG_COMPAT_32BIT_TIME
  1009. SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
  1010. struct old_timespec32 __user *, tp)
  1011. {
  1012. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1013. struct timespec64 ts;
  1014. if (!kc || !kc->clock_set)
  1015. return -EINVAL;
  1016. if (get_old_timespec32(&ts, tp))
  1017. return -EFAULT;
  1018. return kc->clock_set(which_clock, &ts);
  1019. }
  1020. SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
  1021. struct old_timespec32 __user *, tp)
  1022. {
  1023. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1024. struct timespec64 ts;
  1025. int err;
  1026. if (!kc)
  1027. return -EINVAL;
  1028. err = kc->clock_get_timespec(which_clock, &ts);
  1029. if (!err && put_old_timespec32(&ts, tp))
  1030. err = -EFAULT;
  1031. return err;
  1032. }
  1033. SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
  1034. struct old_timex32 __user *, utp)
  1035. {
  1036. struct __kernel_timex ktx;
  1037. int err;
  1038. err = get_old_timex32(&ktx, utp);
  1039. if (err)
  1040. return err;
  1041. err = do_clock_adjtime(which_clock, &ktx);
  1042. if (err >= 0 && put_old_timex32(utp, &ktx))
  1043. return -EFAULT;
  1044. return err;
  1045. }
  1046. SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
  1047. struct old_timespec32 __user *, tp)
  1048. {
  1049. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1050. struct timespec64 ts;
  1051. int err;
  1052. if (!kc)
  1053. return -EINVAL;
  1054. err = kc->clock_getres(which_clock, &ts);
  1055. if (!err && tp && put_old_timespec32(&ts, tp))
  1056. return -EFAULT;
  1057. return err;
  1058. }
  1059. #endif
  1060. /*
  1061. * nanosleep for monotonic and realtime clocks
  1062. */
  1063. static int common_nsleep(const clockid_t which_clock, int flags,
  1064. const struct timespec64 *rqtp)
  1065. {
  1066. ktime_t texp = timespec64_to_ktime(*rqtp);
  1067. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  1068. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  1069. which_clock);
  1070. }
  1071. static int common_nsleep_timens(const clockid_t which_clock, int flags,
  1072. const struct timespec64 *rqtp)
  1073. {
  1074. ktime_t texp = timespec64_to_ktime(*rqtp);
  1075. if (flags & TIMER_ABSTIME)
  1076. texp = timens_ktime_to_host(which_clock, texp);
  1077. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  1078. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  1079. which_clock);
  1080. }
  1081. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  1082. const struct __kernel_timespec __user *, rqtp,
  1083. struct __kernel_timespec __user *, rmtp)
  1084. {
  1085. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1086. struct timespec64 t;
  1087. if (!kc)
  1088. return -EINVAL;
  1089. if (!kc->nsleep)
  1090. return -EOPNOTSUPP;
  1091. if (get_timespec64(&t, rqtp))
  1092. return -EFAULT;
  1093. if (!timespec64_valid(&t))
  1094. return -EINVAL;
  1095. if (flags & TIMER_ABSTIME)
  1096. rmtp = NULL;
  1097. current->restart_block.fn = do_no_restart_syscall;
  1098. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1099. current->restart_block.nanosleep.rmtp = rmtp;
  1100. return kc->nsleep(which_clock, flags, &t);
  1101. }
  1102. #ifdef CONFIG_COMPAT_32BIT_TIME
  1103. SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
  1104. struct old_timespec32 __user *, rqtp,
  1105. struct old_timespec32 __user *, rmtp)
  1106. {
  1107. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1108. struct timespec64 t;
  1109. if (!kc)
  1110. return -EINVAL;
  1111. if (!kc->nsleep)
  1112. return -EOPNOTSUPP;
  1113. if (get_old_timespec32(&t, rqtp))
  1114. return -EFAULT;
  1115. if (!timespec64_valid(&t))
  1116. return -EINVAL;
  1117. if (flags & TIMER_ABSTIME)
  1118. rmtp = NULL;
  1119. current->restart_block.fn = do_no_restart_syscall;
  1120. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1121. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1122. return kc->nsleep(which_clock, flags, &t);
  1123. }
  1124. #endif
  1125. static const struct k_clock clock_realtime = {
  1126. .clock_getres = posix_get_hrtimer_res,
  1127. .clock_get_timespec = posix_get_realtime_timespec,
  1128. .clock_get_ktime = posix_get_realtime_ktime,
  1129. .clock_set = posix_clock_realtime_set,
  1130. .clock_adj = posix_clock_realtime_adj,
  1131. .nsleep = common_nsleep,
  1132. .timer_create = common_timer_create,
  1133. .timer_set = common_timer_set,
  1134. .timer_get = common_timer_get,
  1135. .timer_del = common_timer_del,
  1136. .timer_rearm = common_hrtimer_rearm,
  1137. .timer_forward = common_hrtimer_forward,
  1138. .timer_remaining = common_hrtimer_remaining,
  1139. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1140. .timer_wait_running = common_timer_wait_running,
  1141. .timer_arm = common_hrtimer_arm,
  1142. };
  1143. static const struct k_clock clock_monotonic = {
  1144. .clock_getres = posix_get_hrtimer_res,
  1145. .clock_get_timespec = posix_get_monotonic_timespec,
  1146. .clock_get_ktime = posix_get_monotonic_ktime,
  1147. .nsleep = common_nsleep_timens,
  1148. .timer_create = common_timer_create,
  1149. .timer_set = common_timer_set,
  1150. .timer_get = common_timer_get,
  1151. .timer_del = common_timer_del,
  1152. .timer_rearm = common_hrtimer_rearm,
  1153. .timer_forward = common_hrtimer_forward,
  1154. .timer_remaining = common_hrtimer_remaining,
  1155. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1156. .timer_wait_running = common_timer_wait_running,
  1157. .timer_arm = common_hrtimer_arm,
  1158. };
  1159. static const struct k_clock clock_monotonic_raw = {
  1160. .clock_getres = posix_get_hrtimer_res,
  1161. .clock_get_timespec = posix_get_monotonic_raw,
  1162. };
  1163. static const struct k_clock clock_realtime_coarse = {
  1164. .clock_getres = posix_get_coarse_res,
  1165. .clock_get_timespec = posix_get_realtime_coarse,
  1166. };
  1167. static const struct k_clock clock_monotonic_coarse = {
  1168. .clock_getres = posix_get_coarse_res,
  1169. .clock_get_timespec = posix_get_monotonic_coarse,
  1170. };
  1171. static const struct k_clock clock_tai = {
  1172. .clock_getres = posix_get_hrtimer_res,
  1173. .clock_get_ktime = posix_get_tai_ktime,
  1174. .clock_get_timespec = posix_get_tai_timespec,
  1175. .nsleep = common_nsleep,
  1176. .timer_create = common_timer_create,
  1177. .timer_set = common_timer_set,
  1178. .timer_get = common_timer_get,
  1179. .timer_del = common_timer_del,
  1180. .timer_rearm = common_hrtimer_rearm,
  1181. .timer_forward = common_hrtimer_forward,
  1182. .timer_remaining = common_hrtimer_remaining,
  1183. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1184. .timer_wait_running = common_timer_wait_running,
  1185. .timer_arm = common_hrtimer_arm,
  1186. };
  1187. static const struct k_clock clock_boottime = {
  1188. .clock_getres = posix_get_hrtimer_res,
  1189. .clock_get_ktime = posix_get_boottime_ktime,
  1190. .clock_get_timespec = posix_get_boottime_timespec,
  1191. .nsleep = common_nsleep_timens,
  1192. .timer_create = common_timer_create,
  1193. .timer_set = common_timer_set,
  1194. .timer_get = common_timer_get,
  1195. .timer_del = common_timer_del,
  1196. .timer_rearm = common_hrtimer_rearm,
  1197. .timer_forward = common_hrtimer_forward,
  1198. .timer_remaining = common_hrtimer_remaining,
  1199. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1200. .timer_wait_running = common_timer_wait_running,
  1201. .timer_arm = common_hrtimer_arm,
  1202. };
  1203. static const struct k_clock * const posix_clocks[] = {
  1204. [CLOCK_REALTIME] = &clock_realtime,
  1205. [CLOCK_MONOTONIC] = &clock_monotonic,
  1206. [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
  1207. [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
  1208. [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
  1209. [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
  1210. [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
  1211. [CLOCK_BOOTTIME] = &clock_boottime,
  1212. [CLOCK_REALTIME_ALARM] = &alarm_clock,
  1213. [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
  1214. [CLOCK_TAI] = &clock_tai,
  1215. };
  1216. static const struct k_clock *clockid_to_kclock(const clockid_t id)
  1217. {
  1218. clockid_t idx = id;
  1219. if (id < 0) {
  1220. return (id & CLOCKFD_MASK) == CLOCKFD ?
  1221. &clock_posix_dynamic : &clock_posix_cpu;
  1222. }
  1223. if (id >= ARRAY_SIZE(posix_clocks))
  1224. return NULL;
  1225. return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
  1226. }