posix-timers.c 42 KB

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