posix-timers.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. static struct kmem_cache *posix_timers_cache;
  36. /*
  37. * Timers are managed in a hash table for lockless lookup. The hash key is
  38. * constructed from current::signal and the timer ID and the timer is
  39. * matched against current::signal and the timer ID when walking the hash
  40. * bucket list.
  41. *
  42. * This allows checkpoint/restore to reconstruct the exact timer IDs for
  43. * a process.
  44. */
  45. static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
  46. static DEFINE_SPINLOCK(hash_lock);
  47. static const struct k_clock * const posix_clocks[];
  48. static const struct k_clock *clockid_to_kclock(const clockid_t id);
  49. static const struct k_clock clock_realtime, clock_monotonic;
  50. /* SIGEV_THREAD_ID cannot share a bit with the other SIGEV values. */
  51. #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
  52. ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
  53. #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
  54. #endif
  55. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
  56. #define lock_timer(tid, flags) \
  57. ({ struct k_itimer *__timr; \
  58. __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
  59. __timr; \
  60. })
  61. static int hash(struct signal_struct *sig, unsigned int nr)
  62. {
  63. return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
  64. }
  65. static struct k_itimer *__posix_timers_find(struct hlist_head *head,
  66. struct signal_struct *sig,
  67. timer_t id)
  68. {
  69. struct k_itimer *timer;
  70. hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)) {
  71. /* timer->it_signal can be set concurrently */
  72. if ((READ_ONCE(timer->it_signal) == sig) && (timer->it_id == id))
  73. return timer;
  74. }
  75. return NULL;
  76. }
  77. static struct k_itimer *posix_timer_by_id(timer_t id)
  78. {
  79. struct signal_struct *sig = current->signal;
  80. struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
  81. return __posix_timers_find(head, sig, id);
  82. }
  83. static int posix_timer_add(struct k_itimer *timer)
  84. {
  85. struct signal_struct *sig = current->signal;
  86. struct hlist_head *head;
  87. unsigned int cnt, id;
  88. /*
  89. * FIXME: Replace this by a per signal struct xarray once there is
  90. * a plan to handle the resulting CRIU regression gracefully.
  91. */
  92. for (cnt = 0; cnt <= INT_MAX; cnt++) {
  93. spin_lock(&hash_lock);
  94. id = sig->next_posix_timer_id;
  95. /* Write the next ID back. Clamp it to the positive space */
  96. sig->next_posix_timer_id = (id + 1) & INT_MAX;
  97. head = &posix_timers_hashtable[hash(sig, id)];
  98. if (!__posix_timers_find(head, sig, id)) {
  99. hlist_add_head_rcu(&timer->t_hash, head);
  100. spin_unlock(&hash_lock);
  101. return id;
  102. }
  103. spin_unlock(&hash_lock);
  104. cond_resched();
  105. }
  106. /* POSIX return code when no timer ID could be allocated */
  107. return -EAGAIN;
  108. }
  109. static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
  110. {
  111. spin_unlock_irqrestore(&timr->it_lock, flags);
  112. }
  113. static int posix_get_realtime_timespec(clockid_t which_clock, struct timespec64 *tp)
  114. {
  115. ktime_get_real_ts64(tp);
  116. return 0;
  117. }
  118. static ktime_t posix_get_realtime_ktime(clockid_t which_clock)
  119. {
  120. return ktime_get_real();
  121. }
  122. static int posix_clock_realtime_set(const clockid_t which_clock,
  123. const struct timespec64 *tp)
  124. {
  125. return do_sys_settimeofday64(tp, NULL);
  126. }
  127. static int posix_clock_realtime_adj(const clockid_t which_clock,
  128. struct __kernel_timex *t)
  129. {
  130. return do_adjtimex(t);
  131. }
  132. static int posix_get_monotonic_timespec(clockid_t which_clock, struct timespec64 *tp)
  133. {
  134. ktime_get_ts64(tp);
  135. timens_add_monotonic(tp);
  136. return 0;
  137. }
  138. static ktime_t posix_get_monotonic_ktime(clockid_t which_clock)
  139. {
  140. return ktime_get();
  141. }
  142. static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
  143. {
  144. ktime_get_raw_ts64(tp);
  145. timens_add_monotonic(tp);
  146. return 0;
  147. }
  148. static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
  149. {
  150. ktime_get_coarse_real_ts64(tp);
  151. return 0;
  152. }
  153. static int posix_get_monotonic_coarse(clockid_t which_clock,
  154. struct timespec64 *tp)
  155. {
  156. ktime_get_coarse_ts64(tp);
  157. timens_add_monotonic(tp);
  158. return 0;
  159. }
  160. static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
  161. {
  162. *tp = ktime_to_timespec64(KTIME_LOW_RES);
  163. return 0;
  164. }
  165. static int posix_get_boottime_timespec(const clockid_t which_clock, struct timespec64 *tp)
  166. {
  167. ktime_get_boottime_ts64(tp);
  168. timens_add_boottime(tp);
  169. return 0;
  170. }
  171. static ktime_t posix_get_boottime_ktime(const clockid_t which_clock)
  172. {
  173. return ktime_get_boottime();
  174. }
  175. static int posix_get_tai_timespec(clockid_t which_clock, struct timespec64 *tp)
  176. {
  177. ktime_get_clocktai_ts64(tp);
  178. return 0;
  179. }
  180. static ktime_t posix_get_tai_ktime(clockid_t which_clock)
  181. {
  182. return ktime_get_clocktai();
  183. }
  184. static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
  185. {
  186. tp->tv_sec = 0;
  187. tp->tv_nsec = hrtimer_resolution;
  188. return 0;
  189. }
  190. static __init int init_posix_timers(void)
  191. {
  192. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  193. sizeof(struct k_itimer), 0,
  194. SLAB_PANIC | SLAB_ACCOUNT, NULL);
  195. return 0;
  196. }
  197. __initcall(init_posix_timers);
  198. /*
  199. * The siginfo si_overrun field and the return value of timer_getoverrun(2)
  200. * are of type int. Clamp the overrun value to INT_MAX
  201. */
  202. static inline int timer_overrun_to_int(struct k_itimer *timr, int baseval)
  203. {
  204. s64 sum = timr->it_overrun_last + (s64)baseval;
  205. return sum > (s64)INT_MAX ? INT_MAX : (int)sum;
  206. }
  207. static void common_hrtimer_rearm(struct k_itimer *timr)
  208. {
  209. struct hrtimer *timer = &timr->it.real.timer;
  210. timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(),
  211. timr->it_interval);
  212. hrtimer_restart(timer);
  213. }
  214. /*
  215. * This function is called from the signal delivery code if
  216. * info->si_sys_private is not zero, which indicates that the timer has to
  217. * be rearmed. Restart the timer and update info::si_overrun.
  218. */
  219. void posixtimer_rearm(struct kernel_siginfo *info)
  220. {
  221. struct k_itimer *timr;
  222. unsigned long flags;
  223. timr = lock_timer(info->si_tid, &flags);
  224. if (!timr)
  225. return;
  226. if (timr->it_interval && timr->it_requeue_pending == info->si_sys_private) {
  227. timr->kclock->timer_rearm(timr);
  228. timr->it_active = 1;
  229. timr->it_overrun_last = timr->it_overrun;
  230. timr->it_overrun = -1LL;
  231. ++timr->it_requeue_pending;
  232. info->si_overrun = timer_overrun_to_int(timr, info->si_overrun);
  233. }
  234. unlock_timer(timr, flags);
  235. }
  236. int posix_timer_queue_signal(struct k_itimer *timr)
  237. {
  238. int ret, si_private = 0;
  239. enum pid_type type;
  240. lockdep_assert_held(&timr->it_lock);
  241. timr->it_active = 0;
  242. if (timr->it_interval)
  243. si_private = ++timr->it_requeue_pending;
  244. /*
  245. * FIXME: if ->sigq is queued we can race with
  246. * dequeue_signal()->posixtimer_rearm().
  247. *
  248. * If dequeue_signal() sees the "right" value of
  249. * si_sys_private it calls posixtimer_rearm().
  250. * We re-queue ->sigq and drop ->it_lock().
  251. * posixtimer_rearm() locks the timer
  252. * and re-schedules it while ->sigq is pending.
  253. * Not really bad, but not that we want.
  254. */
  255. timr->sigq->info.si_sys_private = si_private;
  256. type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID;
  257. ret = send_sigqueue(timr->sigq, timr->it_pid, type);
  258. /* If we failed to send the signal the timer stops. */
  259. return ret > 0;
  260. }
  261. /*
  262. * This function gets called when a POSIX.1b interval timer expires from
  263. * the HRTIMER interrupt (soft interrupt on RT kernels).
  264. *
  265. * Handles CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME and CLOCK_TAI
  266. * based timers.
  267. */
  268. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  269. {
  270. struct k_itimer *timr = container_of(timer, struct k_itimer, it.real.timer);
  271. enum hrtimer_restart ret = HRTIMER_NORESTART;
  272. unsigned long flags;
  273. spin_lock_irqsave(&timr->it_lock, flags);
  274. if (posix_timer_queue_signal(timr)) {
  275. /*
  276. * The signal was not queued due to SIG_IGN. As a
  277. * consequence the timer is not going to be rearmed from
  278. * the signal delivery path. But as a real signal handler
  279. * can be installed later the timer must be rearmed here.
  280. */
  281. if (timr->it_interval != 0) {
  282. ktime_t now = hrtimer_cb_get_time(timer);
  283. /*
  284. * FIXME: What we really want, is to stop this
  285. * timer completely and restart it in case the
  286. * SIG_IGN is removed. This is a non trivial
  287. * change to the signal handling code.
  288. *
  289. * For now let timers with an interval less than a
  290. * jiffy expire every jiffy and recheck for a
  291. * valid signal handler.
  292. *
  293. * This avoids interrupt starvation in case of a
  294. * very small interval, which would expire the
  295. * timer immediately again.
  296. *
  297. * Moving now ahead of time by one jiffy tricks
  298. * hrtimer_forward() to expire the timer later,
  299. * while it still maintains the overrun accuracy
  300. * for the price of a slight inconsistency in the
  301. * timer_gettime() case. This is at least better
  302. * than a timer storm.
  303. *
  304. * Only required when high resolution timers are
  305. * enabled as the periodic tick based timers are
  306. * automatically aligned to the next tick.
  307. */
  308. if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) {
  309. ktime_t kj = TICK_NSEC;
  310. if (timr->it_interval < kj)
  311. now = ktime_add(now, kj);
  312. }
  313. timr->it_overrun += hrtimer_forward(timer, now, timr->it_interval);
  314. ret = HRTIMER_RESTART;
  315. ++timr->it_requeue_pending;
  316. timr->it_active = 1;
  317. }
  318. }
  319. unlock_timer(timr, flags);
  320. return ret;
  321. }
  322. static struct pid *good_sigevent(sigevent_t * event)
  323. {
  324. struct pid *pid = task_tgid(current);
  325. struct task_struct *rtn;
  326. switch (event->sigev_notify) {
  327. case SIGEV_SIGNAL | SIGEV_THREAD_ID:
  328. pid = find_vpid(event->sigev_notify_thread_id);
  329. rtn = pid_task(pid, PIDTYPE_PID);
  330. if (!rtn || !same_thread_group(rtn, current))
  331. return NULL;
  332. fallthrough;
  333. case SIGEV_SIGNAL:
  334. case SIGEV_THREAD:
  335. if (event->sigev_signo <= 0 || event->sigev_signo > SIGRTMAX)
  336. return NULL;
  337. fallthrough;
  338. case SIGEV_NONE:
  339. return pid;
  340. default:
  341. return NULL;
  342. }
  343. }
  344. static struct k_itimer * alloc_posix_timer(void)
  345. {
  346. struct k_itimer *tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  347. if (!tmr)
  348. return tmr;
  349. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  350. kmem_cache_free(posix_timers_cache, tmr);
  351. return NULL;
  352. }
  353. clear_siginfo(&tmr->sigq->info);
  354. return tmr;
  355. }
  356. static void k_itimer_rcu_free(struct rcu_head *head)
  357. {
  358. struct k_itimer *tmr = container_of(head, struct k_itimer, rcu);
  359. kmem_cache_free(posix_timers_cache, tmr);
  360. }
  361. static void posix_timer_free(struct k_itimer *tmr)
  362. {
  363. put_pid(tmr->it_pid);
  364. sigqueue_free(tmr->sigq);
  365. call_rcu(&tmr->rcu, k_itimer_rcu_free);
  366. }
  367. static void posix_timer_unhash_and_free(struct k_itimer *tmr)
  368. {
  369. spin_lock(&hash_lock);
  370. hlist_del_rcu(&tmr->t_hash);
  371. spin_unlock(&hash_lock);
  372. posix_timer_free(tmr);
  373. }
  374. static int common_timer_create(struct k_itimer *new_timer)
  375. {
  376. hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
  377. return 0;
  378. }
  379. /* Create a POSIX.1b interval timer. */
  380. static int do_timer_create(clockid_t which_clock, struct sigevent *event,
  381. timer_t __user *created_timer_id)
  382. {
  383. const struct k_clock *kc = clockid_to_kclock(which_clock);
  384. struct k_itimer *new_timer;
  385. int error, new_timer_id;
  386. if (!kc)
  387. return -EINVAL;
  388. if (!kc->timer_create)
  389. return -EOPNOTSUPP;
  390. new_timer = alloc_posix_timer();
  391. if (unlikely(!new_timer))
  392. return -EAGAIN;
  393. spin_lock_init(&new_timer->it_lock);
  394. /*
  395. * Add the timer to the hash table. The timer is not yet valid
  396. * because new_timer::it_signal is still NULL. The timer id is also
  397. * not yet visible to user space.
  398. */
  399. new_timer_id = posix_timer_add(new_timer);
  400. if (new_timer_id < 0) {
  401. posix_timer_free(new_timer);
  402. return new_timer_id;
  403. }
  404. new_timer->it_id = (timer_t) new_timer_id;
  405. new_timer->it_clock = which_clock;
  406. new_timer->kclock = kc;
  407. new_timer->it_overrun = -1LL;
  408. if (event) {
  409. rcu_read_lock();
  410. new_timer->it_pid = get_pid(good_sigevent(event));
  411. rcu_read_unlock();
  412. if (!new_timer->it_pid) {
  413. error = -EINVAL;
  414. goto out;
  415. }
  416. new_timer->it_sigev_notify = event->sigev_notify;
  417. new_timer->sigq->info.si_signo = event->sigev_signo;
  418. new_timer->sigq->info.si_value = event->sigev_value;
  419. } else {
  420. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  421. new_timer->sigq->info.si_signo = SIGALRM;
  422. memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t));
  423. new_timer->sigq->info.si_value.sival_int = new_timer->it_id;
  424. new_timer->it_pid = get_pid(task_tgid(current));
  425. }
  426. new_timer->sigq->info.si_tid = new_timer->it_id;
  427. new_timer->sigq->info.si_code = SI_TIMER;
  428. if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id))) {
  429. error = -EFAULT;
  430. goto out;
  431. }
  432. /*
  433. * After succesful copy out, the timer ID is visible to user space
  434. * now but not yet valid because new_timer::signal is still NULL.
  435. *
  436. * Complete the initialization with the clock specific create
  437. * callback.
  438. */
  439. error = kc->timer_create(new_timer);
  440. if (error)
  441. goto out;
  442. /*
  443. * timer::it_lock ensures that __lock_timer() observes a fully
  444. * initialized timer when it observes a valid timer::it_signal.
  445. *
  446. * sighand::siglock is required to protect signal::posix_timers.
  447. */
  448. scoped_guard (spinlock_irq, &new_timer->it_lock) {
  449. guard(spinlock)(&current->sighand->siglock);
  450. /* This makes the timer valid in the hash table */
  451. WRITE_ONCE(new_timer->it_signal, current->signal);
  452. hlist_add_head(&new_timer->list, &current->signal->posix_timers);
  453. }
  454. /*
  455. * After unlocking @new_timer is subject to concurrent removal and
  456. * cannot be touched anymore
  457. */
  458. return 0;
  459. out:
  460. posix_timer_unhash_and_free(new_timer);
  461. return error;
  462. }
  463. SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
  464. struct sigevent __user *, timer_event_spec,
  465. timer_t __user *, created_timer_id)
  466. {
  467. if (timer_event_spec) {
  468. sigevent_t event;
  469. if (copy_from_user(&event, timer_event_spec, sizeof (event)))
  470. return -EFAULT;
  471. return do_timer_create(which_clock, &event, created_timer_id);
  472. }
  473. return do_timer_create(which_clock, NULL, created_timer_id);
  474. }
  475. #ifdef CONFIG_COMPAT
  476. COMPAT_SYSCALL_DEFINE3(timer_create, clockid_t, which_clock,
  477. struct compat_sigevent __user *, timer_event_spec,
  478. timer_t __user *, created_timer_id)
  479. {
  480. if (timer_event_spec) {
  481. sigevent_t event;
  482. if (get_compat_sigevent(&event, timer_event_spec))
  483. return -EFAULT;
  484. return do_timer_create(which_clock, &event, created_timer_id);
  485. }
  486. return do_timer_create(which_clock, NULL, created_timer_id);
  487. }
  488. #endif
  489. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
  490. {
  491. struct k_itimer *timr;
  492. /*
  493. * timer_t could be any type >= int and we want to make sure any
  494. * @timer_id outside positive int range fails lookup.
  495. */
  496. if ((unsigned long long)timer_id > INT_MAX)
  497. return NULL;
  498. /*
  499. * The hash lookup and the timers are RCU protected.
  500. *
  501. * Timers are added to the hash in invalid state where
  502. * timr::it_signal == NULL. timer::it_signal is only set after the
  503. * rest of the initialization succeeded.
  504. *
  505. * Timer destruction happens in steps:
  506. * 1) Set timr::it_signal to NULL with timr::it_lock held
  507. * 2) Release timr::it_lock
  508. * 3) Remove from the hash under hash_lock
  509. * 4) Call RCU for removal after the grace period
  510. *
  511. * Holding rcu_read_lock() accross the lookup ensures that
  512. * the timer cannot be freed.
  513. *
  514. * The lookup validates locklessly that timr::it_signal ==
  515. * current::it_signal and timr::it_id == @timer_id. timr::it_id
  516. * can't change, but timr::it_signal becomes NULL during
  517. * destruction.
  518. */
  519. rcu_read_lock();
  520. timr = posix_timer_by_id(timer_id);
  521. if (timr) {
  522. spin_lock_irqsave(&timr->it_lock, *flags);
  523. /*
  524. * Validate under timr::it_lock that timr::it_signal is
  525. * still valid. Pairs with #1 above.
  526. */
  527. if (timr->it_signal == current->signal) {
  528. rcu_read_unlock();
  529. return timr;
  530. }
  531. spin_unlock_irqrestore(&timr->it_lock, *flags);
  532. }
  533. rcu_read_unlock();
  534. return NULL;
  535. }
  536. static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
  537. {
  538. struct hrtimer *timer = &timr->it.real.timer;
  539. return __hrtimer_expires_remaining_adjusted(timer, now);
  540. }
  541. static s64 common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
  542. {
  543. struct hrtimer *timer = &timr->it.real.timer;
  544. return hrtimer_forward(timer, now, timr->it_interval);
  545. }
  546. /*
  547. * Get the time remaining on a POSIX.1b interval timer.
  548. *
  549. * Two issues to handle here:
  550. *
  551. * 1) The timer has a requeue pending. The return value must appear as
  552. * if the timer has been requeued right now.
  553. *
  554. * 2) The timer is a SIGEV_NONE timer. These timers are never enqueued
  555. * into the hrtimer queue and therefore never expired. Emulate expiry
  556. * here taking #1 into account.
  557. */
  558. void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
  559. {
  560. const struct k_clock *kc = timr->kclock;
  561. ktime_t now, remaining, iv;
  562. bool sig_none;
  563. sig_none = timr->it_sigev_notify == SIGEV_NONE;
  564. iv = timr->it_interval;
  565. /* interval timer ? */
  566. if (iv) {
  567. cur_setting->it_interval = ktime_to_timespec64(iv);
  568. } else if (!timr->it_active) {
  569. /*
  570. * SIGEV_NONE oneshot timers are never queued and therefore
  571. * timr->it_active is always false. The check below
  572. * vs. remaining time will handle this case.
  573. *
  574. * For all other timers there is nothing to update here, so
  575. * return.
  576. */
  577. if (!sig_none)
  578. return;
  579. }
  580. now = kc->clock_get_ktime(timr->it_clock);
  581. /*
  582. * If this is an interval timer and either has requeue pending or
  583. * is a SIGEV_NONE timer move the expiry time forward by intervals,
  584. * so expiry is > now.
  585. */
  586. if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
  587. timr->it_overrun += kc->timer_forward(timr, now);
  588. remaining = kc->timer_remaining(timr, now);
  589. /*
  590. * As @now is retrieved before a possible timer_forward() and
  591. * cannot be reevaluated by the compiler @remaining is based on the
  592. * same @now value. Therefore @remaining is consistent vs. @now.
  593. *
  594. * Consequently all interval timers, i.e. @iv > 0, cannot have a
  595. * remaining time <= 0 because timer_forward() guarantees to move
  596. * them forward so that the next timer expiry is > @now.
  597. */
  598. if (remaining <= 0) {
  599. /*
  600. * A single shot SIGEV_NONE timer must return 0, when it is
  601. * expired! Timers which have a real signal delivery mode
  602. * must return a remaining time greater than 0 because the
  603. * signal has not yet been delivered.
  604. */
  605. if (!sig_none)
  606. cur_setting->it_value.tv_nsec = 1;
  607. } else {
  608. cur_setting->it_value = ktime_to_timespec64(remaining);
  609. }
  610. }
  611. static int do_timer_gettime(timer_t timer_id, struct itimerspec64 *setting)
  612. {
  613. const struct k_clock *kc;
  614. struct k_itimer *timr;
  615. unsigned long flags;
  616. int ret = 0;
  617. timr = lock_timer(timer_id, &flags);
  618. if (!timr)
  619. return -EINVAL;
  620. memset(setting, 0, sizeof(*setting));
  621. kc = timr->kclock;
  622. if (WARN_ON_ONCE(!kc || !kc->timer_get))
  623. ret = -EINVAL;
  624. else
  625. kc->timer_get(timr, setting);
  626. unlock_timer(timr, flags);
  627. return ret;
  628. }
  629. /* Get the time remaining on a POSIX.1b interval timer. */
  630. SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
  631. struct __kernel_itimerspec __user *, setting)
  632. {
  633. struct itimerspec64 cur_setting;
  634. int ret = do_timer_gettime(timer_id, &cur_setting);
  635. if (!ret) {
  636. if (put_itimerspec64(&cur_setting, setting))
  637. ret = -EFAULT;
  638. }
  639. return ret;
  640. }
  641. #ifdef CONFIG_COMPAT_32BIT_TIME
  642. SYSCALL_DEFINE2(timer_gettime32, timer_t, timer_id,
  643. struct old_itimerspec32 __user *, setting)
  644. {
  645. struct itimerspec64 cur_setting;
  646. int ret = do_timer_gettime(timer_id, &cur_setting);
  647. if (!ret) {
  648. if (put_old_itimerspec32(&cur_setting, setting))
  649. ret = -EFAULT;
  650. }
  651. return ret;
  652. }
  653. #endif
  654. /**
  655. * sys_timer_getoverrun - Get the number of overruns of a POSIX.1b interval timer
  656. * @timer_id: The timer ID which identifies the timer
  657. *
  658. * The "overrun count" of a timer is one plus the number of expiration
  659. * intervals which have elapsed between the first expiry, which queues the
  660. * signal and the actual signal delivery. On signal delivery the "overrun
  661. * count" is calculated and cached, so it can be returned directly here.
  662. *
  663. * As this is relative to the last queued signal the returned overrun count
  664. * is meaningless outside of the signal delivery path and even there it
  665. * does not accurately reflect the current state when user space evaluates
  666. * it.
  667. *
  668. * Returns:
  669. * -EINVAL @timer_id is invalid
  670. * 1..INT_MAX The number of overruns related to the last delivered signal
  671. */
  672. SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  673. {
  674. struct k_itimer *timr;
  675. unsigned long flags;
  676. int overrun;
  677. timr = lock_timer(timer_id, &flags);
  678. if (!timr)
  679. return -EINVAL;
  680. overrun = timer_overrun_to_int(timr, 0);
  681. unlock_timer(timr, flags);
  682. return overrun;
  683. }
  684. static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
  685. bool absolute, bool sigev_none)
  686. {
  687. struct hrtimer *timer = &timr->it.real.timer;
  688. enum hrtimer_mode mode;
  689. mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  690. /*
  691. * Posix magic: Relative CLOCK_REALTIME timers are not affected by
  692. * clock modifications, so they become CLOCK_MONOTONIC based under the
  693. * hood. See hrtimer_init(). Update timr->kclock, so the generic
  694. * functions which use timr->kclock->clock_get_*() work.
  695. *
  696. * Note: it_clock stays unmodified, because the next timer_set() might
  697. * use ABSTIME, so it needs to switch back.
  698. */
  699. if (timr->it_clock == CLOCK_REALTIME)
  700. timr->kclock = absolute ? &clock_realtime : &clock_monotonic;
  701. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  702. timr->it.real.timer.function = posix_timer_fn;
  703. if (!absolute)
  704. expires = ktime_add_safe(expires, timer->base->get_time());
  705. hrtimer_set_expires(timer, expires);
  706. if (!sigev_none)
  707. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  708. }
  709. static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
  710. {
  711. return hrtimer_try_to_cancel(&timr->it.real.timer);
  712. }
  713. static void common_timer_wait_running(struct k_itimer *timer)
  714. {
  715. hrtimer_cancel_wait_running(&timer->it.real.timer);
  716. }
  717. /*
  718. * On PREEMPT_RT this prevents priority inversion and a potential livelock
  719. * against the ksoftirqd thread in case that ksoftirqd gets preempted while
  720. * executing a hrtimer callback.
  721. *
  722. * See the comments in hrtimer_cancel_wait_running(). For PREEMPT_RT=n this
  723. * just results in a cpu_relax().
  724. *
  725. * For POSIX CPU timers with CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n this is
  726. * just a cpu_relax(). With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y this
  727. * prevents spinning on an eventually scheduled out task and a livelock
  728. * when the task which tries to delete or disarm the timer has preempted
  729. * the task which runs the expiry in task work context.
  730. */
  731. static struct k_itimer *timer_wait_running(struct k_itimer *timer,
  732. unsigned long *flags)
  733. {
  734. const struct k_clock *kc = READ_ONCE(timer->kclock);
  735. timer_t timer_id = READ_ONCE(timer->it_id);
  736. /* Prevent kfree(timer) after dropping the lock */
  737. rcu_read_lock();
  738. unlock_timer(timer, *flags);
  739. /*
  740. * kc->timer_wait_running() might drop RCU lock. So @timer
  741. * cannot be touched anymore after the function returns!
  742. */
  743. if (!WARN_ON_ONCE(!kc->timer_wait_running))
  744. kc->timer_wait_running(timer);
  745. rcu_read_unlock();
  746. /* Relock the timer. It might be not longer hashed. */
  747. return lock_timer(timer_id, flags);
  748. }
  749. /*
  750. * Set up the new interval and reset the signal delivery data
  751. */
  752. void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting)
  753. {
  754. if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec)
  755. timer->it_interval = timespec64_to_ktime(new_setting->it_interval);
  756. else
  757. timer->it_interval = 0;
  758. /* Prevent reloading in case there is a signal pending */
  759. timer->it_requeue_pending = (timer->it_requeue_pending + 2) & ~REQUEUE_PENDING;
  760. /* Reset overrun accounting */
  761. timer->it_overrun_last = 0;
  762. timer->it_overrun = -1LL;
  763. }
  764. /* Set a POSIX.1b interval timer. */
  765. int common_timer_set(struct k_itimer *timr, int flags,
  766. struct itimerspec64 *new_setting,
  767. struct itimerspec64 *old_setting)
  768. {
  769. const struct k_clock *kc = timr->kclock;
  770. bool sigev_none;
  771. ktime_t expires;
  772. if (old_setting)
  773. common_timer_get(timr, old_setting);
  774. /* Prevent rearming by clearing the interval */
  775. timr->it_interval = 0;
  776. /*
  777. * Careful here. On SMP systems the timer expiry function could be
  778. * active and spinning on timr->it_lock.
  779. */
  780. if (kc->timer_try_to_cancel(timr) < 0)
  781. return TIMER_RETRY;
  782. timr->it_active = 0;
  783. posix_timer_set_common(timr, new_setting);
  784. /* Keep timer disarmed when it_value is zero */
  785. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  786. return 0;
  787. expires = timespec64_to_ktime(new_setting->it_value);
  788. if (flags & TIMER_ABSTIME)
  789. expires = timens_ktime_to_host(timr->it_clock, expires);
  790. sigev_none = timr->it_sigev_notify == SIGEV_NONE;
  791. kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
  792. timr->it_active = !sigev_none;
  793. return 0;
  794. }
  795. static int do_timer_settime(timer_t timer_id, int tmr_flags,
  796. struct itimerspec64 *new_spec64,
  797. struct itimerspec64 *old_spec64)
  798. {
  799. const struct k_clock *kc;
  800. struct k_itimer *timr;
  801. unsigned long flags;
  802. int error;
  803. if (!timespec64_valid(&new_spec64->it_interval) ||
  804. !timespec64_valid(&new_spec64->it_value))
  805. return -EINVAL;
  806. if (old_spec64)
  807. memset(old_spec64, 0, sizeof(*old_spec64));
  808. timr = lock_timer(timer_id, &flags);
  809. retry:
  810. if (!timr)
  811. return -EINVAL;
  812. if (old_spec64)
  813. old_spec64->it_interval = ktime_to_timespec64(timr->it_interval);
  814. kc = timr->kclock;
  815. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  816. error = -EINVAL;
  817. else
  818. error = kc->timer_set(timr, tmr_flags, new_spec64, old_spec64);
  819. if (error == TIMER_RETRY) {
  820. // We already got the old time...
  821. old_spec64 = NULL;
  822. /* Unlocks and relocks the timer if it still exists */
  823. timr = timer_wait_running(timr, &flags);
  824. goto retry;
  825. }
  826. unlock_timer(timr, flags);
  827. return error;
  828. }
  829. /* Set a POSIX.1b interval timer */
  830. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  831. const struct __kernel_itimerspec __user *, new_setting,
  832. struct __kernel_itimerspec __user *, old_setting)
  833. {
  834. struct itimerspec64 new_spec, old_spec, *rtn;
  835. int error = 0;
  836. if (!new_setting)
  837. return -EINVAL;
  838. if (get_itimerspec64(&new_spec, new_setting))
  839. return -EFAULT;
  840. rtn = old_setting ? &old_spec : NULL;
  841. error = do_timer_settime(timer_id, flags, &new_spec, rtn);
  842. if (!error && old_setting) {
  843. if (put_itimerspec64(&old_spec, old_setting))
  844. error = -EFAULT;
  845. }
  846. return error;
  847. }
  848. #ifdef CONFIG_COMPAT_32BIT_TIME
  849. SYSCALL_DEFINE4(timer_settime32, timer_t, timer_id, int, flags,
  850. struct old_itimerspec32 __user *, new,
  851. struct old_itimerspec32 __user *, old)
  852. {
  853. struct itimerspec64 new_spec, old_spec;
  854. struct itimerspec64 *rtn = old ? &old_spec : NULL;
  855. int error = 0;
  856. if (!new)
  857. return -EINVAL;
  858. if (get_old_itimerspec32(&new_spec, new))
  859. return -EFAULT;
  860. error = do_timer_settime(timer_id, flags, &new_spec, rtn);
  861. if (!error && old) {
  862. if (put_old_itimerspec32(&old_spec, old))
  863. error = -EFAULT;
  864. }
  865. return error;
  866. }
  867. #endif
  868. int common_timer_del(struct k_itimer *timer)
  869. {
  870. const struct k_clock *kc = timer->kclock;
  871. timer->it_interval = 0;
  872. if (kc->timer_try_to_cancel(timer) < 0)
  873. return TIMER_RETRY;
  874. timer->it_active = 0;
  875. return 0;
  876. }
  877. static inline int timer_delete_hook(struct k_itimer *timer)
  878. {
  879. const struct k_clock *kc = timer->kclock;
  880. if (WARN_ON_ONCE(!kc || !kc->timer_del))
  881. return -EINVAL;
  882. return kc->timer_del(timer);
  883. }
  884. /* Delete a POSIX.1b interval timer. */
  885. SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  886. {
  887. struct k_itimer *timer;
  888. unsigned long flags;
  889. timer = lock_timer(timer_id, &flags);
  890. retry_delete:
  891. if (!timer)
  892. return -EINVAL;
  893. if (unlikely(timer_delete_hook(timer) == TIMER_RETRY)) {
  894. /* Unlocks and relocks the timer if it still exists */
  895. timer = timer_wait_running(timer, &flags);
  896. goto retry_delete;
  897. }
  898. spin_lock(&current->sighand->siglock);
  899. hlist_del(&timer->list);
  900. spin_unlock(&current->sighand->siglock);
  901. /*
  902. * A concurrent lookup could check timer::it_signal lockless. It
  903. * will reevaluate with timer::it_lock held and observe the NULL.
  904. */
  905. WRITE_ONCE(timer->it_signal, NULL);
  906. unlock_timer(timer, flags);
  907. posix_timer_unhash_and_free(timer);
  908. return 0;
  909. }
  910. /*
  911. * Delete a timer if it is armed, remove it from the hash and schedule it
  912. * for RCU freeing.
  913. */
  914. static void itimer_delete(struct k_itimer *timer)
  915. {
  916. unsigned long flags;
  917. /*
  918. * irqsave is required to make timer_wait_running() work.
  919. */
  920. spin_lock_irqsave(&timer->it_lock, flags);
  921. retry_delete:
  922. /*
  923. * Even if the timer is not longer accessible from other tasks
  924. * it still might be armed and queued in the underlying timer
  925. * mechanism. Worse, that timer mechanism might run the expiry
  926. * function concurrently.
  927. */
  928. if (timer_delete_hook(timer) == TIMER_RETRY) {
  929. /*
  930. * Timer is expired concurrently, prevent livelocks
  931. * and pointless spinning on RT.
  932. *
  933. * timer_wait_running() drops timer::it_lock, which opens
  934. * the possibility for another task to delete the timer.
  935. *
  936. * That's not possible here because this is invoked from
  937. * do_exit() only for the last thread of the thread group.
  938. * So no other task can access and delete that timer.
  939. */
  940. if (WARN_ON_ONCE(timer_wait_running(timer, &flags) != timer))
  941. return;
  942. goto retry_delete;
  943. }
  944. hlist_del(&timer->list);
  945. /*
  946. * Setting timer::it_signal to NULL is technically not required
  947. * here as nothing can access the timer anymore legitimately via
  948. * the hash table. Set it to NULL nevertheless so that all deletion
  949. * paths are consistent.
  950. */
  951. WRITE_ONCE(timer->it_signal, NULL);
  952. spin_unlock_irqrestore(&timer->it_lock, flags);
  953. posix_timer_unhash_and_free(timer);
  954. }
  955. /*
  956. * Invoked from do_exit() when the last thread of a thread group exits.
  957. * At that point no other task can access the timers of the dying
  958. * task anymore.
  959. */
  960. void exit_itimers(struct task_struct *tsk)
  961. {
  962. struct hlist_head timers;
  963. if (hlist_empty(&tsk->signal->posix_timers))
  964. return;
  965. /* Protect against concurrent read via /proc/$PID/timers */
  966. spin_lock_irq(&tsk->sighand->siglock);
  967. hlist_move_list(&tsk->signal->posix_timers, &timers);
  968. spin_unlock_irq(&tsk->sighand->siglock);
  969. /* The timers are not longer accessible via tsk::signal */
  970. while (!hlist_empty(&timers))
  971. itimer_delete(hlist_entry(timers.first, struct k_itimer, list));
  972. }
  973. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  974. const struct __kernel_timespec __user *, tp)
  975. {
  976. const struct k_clock *kc = clockid_to_kclock(which_clock);
  977. struct timespec64 new_tp;
  978. if (!kc || !kc->clock_set)
  979. return -EINVAL;
  980. if (get_timespec64(&new_tp, tp))
  981. return -EFAULT;
  982. /*
  983. * Permission checks have to be done inside the clock specific
  984. * setter callback.
  985. */
  986. return kc->clock_set(which_clock, &new_tp);
  987. }
  988. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  989. struct __kernel_timespec __user *, tp)
  990. {
  991. const struct k_clock *kc = clockid_to_kclock(which_clock);
  992. struct timespec64 kernel_tp;
  993. int error;
  994. if (!kc)
  995. return -EINVAL;
  996. error = kc->clock_get_timespec(which_clock, &kernel_tp);
  997. if (!error && put_timespec64(&kernel_tp, tp))
  998. error = -EFAULT;
  999. return error;
  1000. }
  1001. int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx)
  1002. {
  1003. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1004. if (!kc)
  1005. return -EINVAL;
  1006. if (!kc->clock_adj)
  1007. return -EOPNOTSUPP;
  1008. return kc->clock_adj(which_clock, ktx);
  1009. }
  1010. SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
  1011. struct __kernel_timex __user *, utx)
  1012. {
  1013. struct __kernel_timex ktx;
  1014. int err;
  1015. if (copy_from_user(&ktx, utx, sizeof(ktx)))
  1016. return -EFAULT;
  1017. err = do_clock_adjtime(which_clock, &ktx);
  1018. if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
  1019. return -EFAULT;
  1020. return err;
  1021. }
  1022. /**
  1023. * sys_clock_getres - Get the resolution of a clock
  1024. * @which_clock: The clock to get the resolution for
  1025. * @tp: Pointer to a a user space timespec64 for storage
  1026. *
  1027. * POSIX defines:
  1028. *
  1029. * "The clock_getres() function shall return the resolution of any
  1030. * clock. Clock resolutions are implementation-defined and cannot be set by
  1031. * a process. If the argument res is not NULL, the resolution of the
  1032. * specified clock shall be stored in the location pointed to by res. If
  1033. * res is NULL, the clock resolution is not returned. If the time argument
  1034. * of clock_settime() is not a multiple of res, then the value is truncated
  1035. * to a multiple of res."
  1036. *
  1037. * Due to the various hardware constraints the real resolution can vary
  1038. * wildly and even change during runtime when the underlying devices are
  1039. * replaced. The kernel also can use hardware devices with different
  1040. * resolutions for reading the time and for arming timers.
  1041. *
  1042. * The kernel therefore deviates from the POSIX spec in various aspects:
  1043. *
  1044. * 1) The resolution returned to user space
  1045. *
  1046. * For CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_TAI,
  1047. * CLOCK_REALTIME_ALARM, CLOCK_BOOTTIME_ALAREM and CLOCK_MONOTONIC_RAW
  1048. * the kernel differentiates only two cases:
  1049. *
  1050. * I) Low resolution mode:
  1051. *
  1052. * When high resolution timers are disabled at compile or runtime
  1053. * the resolution returned is nanoseconds per tick, which represents
  1054. * the precision at which timers expire.
  1055. *
  1056. * II) High resolution mode:
  1057. *
  1058. * When high resolution timers are enabled the resolution returned
  1059. * is always one nanosecond independent of the actual resolution of
  1060. * the underlying hardware devices.
  1061. *
  1062. * For CLOCK_*_ALARM the actual resolution depends on system
  1063. * state. When system is running the resolution is the same as the
  1064. * resolution of the other clocks. During suspend the actual
  1065. * resolution is the resolution of the underlying RTC device which
  1066. * might be way less precise than the clockevent device used during
  1067. * running state.
  1068. *
  1069. * For CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE the resolution
  1070. * returned is always nanoseconds per tick.
  1071. *
  1072. * For CLOCK_PROCESS_CPUTIME and CLOCK_THREAD_CPUTIME the resolution
  1073. * returned is always one nanosecond under the assumption that the
  1074. * underlying scheduler clock has a better resolution than nanoseconds
  1075. * per tick.
  1076. *
  1077. * For dynamic POSIX clocks (PTP devices) the resolution returned is
  1078. * always one nanosecond.
  1079. *
  1080. * 2) Affect on sys_clock_settime()
  1081. *
  1082. * The kernel does not truncate the time which is handed in to
  1083. * sys_clock_settime(). The kernel internal timekeeping is always using
  1084. * nanoseconds precision independent of the clocksource device which is
  1085. * used to read the time from. The resolution of that device only
  1086. * affects the presicion of the time returned by sys_clock_gettime().
  1087. *
  1088. * Returns:
  1089. * 0 Success. @tp contains the resolution
  1090. * -EINVAL @which_clock is not a valid clock ID
  1091. * -EFAULT Copying the resolution to @tp faulted
  1092. * -ENODEV Dynamic POSIX clock is not backed by a device
  1093. * -EOPNOTSUPP Dynamic POSIX clock does not support getres()
  1094. */
  1095. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
  1096. struct __kernel_timespec __user *, tp)
  1097. {
  1098. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1099. struct timespec64 rtn_tp;
  1100. int error;
  1101. if (!kc)
  1102. return -EINVAL;
  1103. error = kc->clock_getres(which_clock, &rtn_tp);
  1104. if (!error && tp && put_timespec64(&rtn_tp, tp))
  1105. error = -EFAULT;
  1106. return error;
  1107. }
  1108. #ifdef CONFIG_COMPAT_32BIT_TIME
  1109. SYSCALL_DEFINE2(clock_settime32, clockid_t, which_clock,
  1110. struct old_timespec32 __user *, tp)
  1111. {
  1112. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1113. struct timespec64 ts;
  1114. if (!kc || !kc->clock_set)
  1115. return -EINVAL;
  1116. if (get_old_timespec32(&ts, tp))
  1117. return -EFAULT;
  1118. return kc->clock_set(which_clock, &ts);
  1119. }
  1120. SYSCALL_DEFINE2(clock_gettime32, clockid_t, which_clock,
  1121. struct old_timespec32 __user *, tp)
  1122. {
  1123. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1124. struct timespec64 ts;
  1125. int err;
  1126. if (!kc)
  1127. return -EINVAL;
  1128. err = kc->clock_get_timespec(which_clock, &ts);
  1129. if (!err && put_old_timespec32(&ts, tp))
  1130. err = -EFAULT;
  1131. return err;
  1132. }
  1133. SYSCALL_DEFINE2(clock_adjtime32, clockid_t, which_clock,
  1134. struct old_timex32 __user *, utp)
  1135. {
  1136. struct __kernel_timex ktx;
  1137. int err;
  1138. err = get_old_timex32(&ktx, utp);
  1139. if (err)
  1140. return err;
  1141. err = do_clock_adjtime(which_clock, &ktx);
  1142. if (err >= 0 && put_old_timex32(utp, &ktx))
  1143. return -EFAULT;
  1144. return err;
  1145. }
  1146. SYSCALL_DEFINE2(clock_getres_time32, clockid_t, which_clock,
  1147. struct old_timespec32 __user *, tp)
  1148. {
  1149. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1150. struct timespec64 ts;
  1151. int err;
  1152. if (!kc)
  1153. return -EINVAL;
  1154. err = kc->clock_getres(which_clock, &ts);
  1155. if (!err && tp && put_old_timespec32(&ts, tp))
  1156. return -EFAULT;
  1157. return err;
  1158. }
  1159. #endif
  1160. /*
  1161. * sys_clock_nanosleep() for CLOCK_REALTIME and CLOCK_TAI
  1162. */
  1163. static int common_nsleep(const clockid_t which_clock, int flags,
  1164. const struct timespec64 *rqtp)
  1165. {
  1166. ktime_t texp = timespec64_to_ktime(*rqtp);
  1167. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  1168. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  1169. which_clock);
  1170. }
  1171. /*
  1172. * sys_clock_nanosleep() for CLOCK_MONOTONIC and CLOCK_BOOTTIME
  1173. *
  1174. * Absolute nanosleeps for these clocks are time-namespace adjusted.
  1175. */
  1176. static int common_nsleep_timens(const clockid_t which_clock, int flags,
  1177. const struct timespec64 *rqtp)
  1178. {
  1179. ktime_t texp = timespec64_to_ktime(*rqtp);
  1180. if (flags & TIMER_ABSTIME)
  1181. texp = timens_ktime_to_host(which_clock, texp);
  1182. return hrtimer_nanosleep(texp, flags & TIMER_ABSTIME ?
  1183. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  1184. which_clock);
  1185. }
  1186. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  1187. const struct __kernel_timespec __user *, rqtp,
  1188. struct __kernel_timespec __user *, rmtp)
  1189. {
  1190. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1191. struct timespec64 t;
  1192. if (!kc)
  1193. return -EINVAL;
  1194. if (!kc->nsleep)
  1195. return -EOPNOTSUPP;
  1196. if (get_timespec64(&t, rqtp))
  1197. return -EFAULT;
  1198. if (!timespec64_valid(&t))
  1199. return -EINVAL;
  1200. if (flags & TIMER_ABSTIME)
  1201. rmtp = NULL;
  1202. current->restart_block.fn = do_no_restart_syscall;
  1203. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1204. current->restart_block.nanosleep.rmtp = rmtp;
  1205. return kc->nsleep(which_clock, flags, &t);
  1206. }
  1207. #ifdef CONFIG_COMPAT_32BIT_TIME
  1208. SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags,
  1209. struct old_timespec32 __user *, rqtp,
  1210. struct old_timespec32 __user *, rmtp)
  1211. {
  1212. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1213. struct timespec64 t;
  1214. if (!kc)
  1215. return -EINVAL;
  1216. if (!kc->nsleep)
  1217. return -EOPNOTSUPP;
  1218. if (get_old_timespec32(&t, rqtp))
  1219. return -EFAULT;
  1220. if (!timespec64_valid(&t))
  1221. return -EINVAL;
  1222. if (flags & TIMER_ABSTIME)
  1223. rmtp = NULL;
  1224. current->restart_block.fn = do_no_restart_syscall;
  1225. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1226. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1227. return kc->nsleep(which_clock, flags, &t);
  1228. }
  1229. #endif
  1230. static const struct k_clock clock_realtime = {
  1231. .clock_getres = posix_get_hrtimer_res,
  1232. .clock_get_timespec = posix_get_realtime_timespec,
  1233. .clock_get_ktime = posix_get_realtime_ktime,
  1234. .clock_set = posix_clock_realtime_set,
  1235. .clock_adj = posix_clock_realtime_adj,
  1236. .nsleep = common_nsleep,
  1237. .timer_create = common_timer_create,
  1238. .timer_set = common_timer_set,
  1239. .timer_get = common_timer_get,
  1240. .timer_del = common_timer_del,
  1241. .timer_rearm = common_hrtimer_rearm,
  1242. .timer_forward = common_hrtimer_forward,
  1243. .timer_remaining = common_hrtimer_remaining,
  1244. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1245. .timer_wait_running = common_timer_wait_running,
  1246. .timer_arm = common_hrtimer_arm,
  1247. };
  1248. static const struct k_clock clock_monotonic = {
  1249. .clock_getres = posix_get_hrtimer_res,
  1250. .clock_get_timespec = posix_get_monotonic_timespec,
  1251. .clock_get_ktime = posix_get_monotonic_ktime,
  1252. .nsleep = common_nsleep_timens,
  1253. .timer_create = common_timer_create,
  1254. .timer_set = common_timer_set,
  1255. .timer_get = common_timer_get,
  1256. .timer_del = common_timer_del,
  1257. .timer_rearm = common_hrtimer_rearm,
  1258. .timer_forward = common_hrtimer_forward,
  1259. .timer_remaining = common_hrtimer_remaining,
  1260. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1261. .timer_wait_running = common_timer_wait_running,
  1262. .timer_arm = common_hrtimer_arm,
  1263. };
  1264. static const struct k_clock clock_monotonic_raw = {
  1265. .clock_getres = posix_get_hrtimer_res,
  1266. .clock_get_timespec = posix_get_monotonic_raw,
  1267. };
  1268. static const struct k_clock clock_realtime_coarse = {
  1269. .clock_getres = posix_get_coarse_res,
  1270. .clock_get_timespec = posix_get_realtime_coarse,
  1271. };
  1272. static const struct k_clock clock_monotonic_coarse = {
  1273. .clock_getres = posix_get_coarse_res,
  1274. .clock_get_timespec = posix_get_monotonic_coarse,
  1275. };
  1276. static const struct k_clock clock_tai = {
  1277. .clock_getres = posix_get_hrtimer_res,
  1278. .clock_get_ktime = posix_get_tai_ktime,
  1279. .clock_get_timespec = posix_get_tai_timespec,
  1280. .nsleep = common_nsleep,
  1281. .timer_create = common_timer_create,
  1282. .timer_set = common_timer_set,
  1283. .timer_get = common_timer_get,
  1284. .timer_del = common_timer_del,
  1285. .timer_rearm = common_hrtimer_rearm,
  1286. .timer_forward = common_hrtimer_forward,
  1287. .timer_remaining = common_hrtimer_remaining,
  1288. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1289. .timer_wait_running = common_timer_wait_running,
  1290. .timer_arm = common_hrtimer_arm,
  1291. };
  1292. static const struct k_clock clock_boottime = {
  1293. .clock_getres = posix_get_hrtimer_res,
  1294. .clock_get_ktime = posix_get_boottime_ktime,
  1295. .clock_get_timespec = posix_get_boottime_timespec,
  1296. .nsleep = common_nsleep_timens,
  1297. .timer_create = common_timer_create,
  1298. .timer_set = common_timer_set,
  1299. .timer_get = common_timer_get,
  1300. .timer_del = common_timer_del,
  1301. .timer_rearm = common_hrtimer_rearm,
  1302. .timer_forward = common_hrtimer_forward,
  1303. .timer_remaining = common_hrtimer_remaining,
  1304. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1305. .timer_wait_running = common_timer_wait_running,
  1306. .timer_arm = common_hrtimer_arm,
  1307. };
  1308. static const struct k_clock * const posix_clocks[] = {
  1309. [CLOCK_REALTIME] = &clock_realtime,
  1310. [CLOCK_MONOTONIC] = &clock_monotonic,
  1311. [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
  1312. [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
  1313. [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
  1314. [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
  1315. [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
  1316. [CLOCK_BOOTTIME] = &clock_boottime,
  1317. [CLOCK_REALTIME_ALARM] = &alarm_clock,
  1318. [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
  1319. [CLOCK_TAI] = &clock_tai,
  1320. };
  1321. static const struct k_clock *clockid_to_kclock(const clockid_t id)
  1322. {
  1323. clockid_t idx = id;
  1324. if (id < 0) {
  1325. return (id & CLOCKFD_MASK) == CLOCKFD ?
  1326. &clock_posix_dynamic : &clock_posix_cpu;
  1327. }
  1328. if (id >= ARRAY_SIZE(posix_clocks))
  1329. return NULL;
  1330. return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
  1331. }