posix-timers.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * linux/kernel/posix-timers.c
  3. *
  4. *
  5. * 2002-10-15 Posix Clocks & timers
  6. * by George Anzinger george@mvista.com
  7. *
  8. * Copyright (C) 2002 2003 by MontaVista Software.
  9. *
  10. * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
  11. * Copyright (C) 2004 Boris Hu
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
  27. */
  28. /* These are all the functions necessary to implement
  29. * POSIX clocks & timers
  30. */
  31. #include <linux/mm.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/mutex.h>
  36. #include <linux/sched/task.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/list.h>
  39. #include <linux/init.h>
  40. #include <linux/compiler.h>
  41. #include <linux/hash.h>
  42. #include <linux/posix-clock.h>
  43. #include <linux/posix-timers.h>
  44. #include <linux/syscalls.h>
  45. #include <linux/wait.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/export.h>
  48. #include <linux/hashtable.h>
  49. #include "timekeeping.h"
  50. /*
  51. * Management arrays for POSIX timers. Timers are now kept in static hash table
  52. * with 512 entries.
  53. * Timer ids are allocated by local routine, which selects proper hash head by
  54. * key, constructed from current->signal address and per signal struct counter.
  55. * This keeps timer ids unique per process, but now they can intersect between
  56. * processes.
  57. */
  58. /*
  59. * Lets keep our timers in a slab cache :-)
  60. */
  61. static struct kmem_cache *posix_timers_cache;
  62. static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
  63. static DEFINE_SPINLOCK(hash_lock);
  64. /*
  65. * we assume that the new SIGEV_THREAD_ID shares no bits with the other
  66. * SIGEV values. Here we put out an error if this assumption fails.
  67. */
  68. #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
  69. ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
  70. #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
  71. #endif
  72. /*
  73. * parisc wants ENOTSUP instead of EOPNOTSUPP
  74. */
  75. #ifndef ENOTSUP
  76. # define ENANOSLEEP_NOTSUP EOPNOTSUPP
  77. #else
  78. # define ENANOSLEEP_NOTSUP ENOTSUP
  79. #endif
  80. /*
  81. * The timer ID is turned into a timer address by idr_find().
  82. * Verifying a valid ID consists of:
  83. *
  84. * a) checking that idr_find() returns other than -1.
  85. * b) checking that the timer id matches the one in the timer itself.
  86. * c) that the timer owner is in the callers thread group.
  87. */
  88. /*
  89. * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
  90. * to implement others. This structure defines the various
  91. * clocks.
  92. *
  93. * RESOLUTION: Clock resolution is used to round up timer and interval
  94. * times, NOT to report clock times, which are reported with as
  95. * much resolution as the system can muster. In some cases this
  96. * resolution may depend on the underlying clock hardware and
  97. * may not be quantifiable until run time, and only then is the
  98. * necessary code is written. The standard says we should say
  99. * something about this issue in the documentation...
  100. *
  101. * FUNCTIONS: The CLOCKs structure defines possible functions to
  102. * handle various clock functions.
  103. *
  104. * The standard POSIX timer management code assumes the
  105. * following: 1.) The k_itimer struct (sched.h) is used for
  106. * the timer. 2.) The list, it_lock, it_clock, it_id and
  107. * it_pid fields are not modified by timer code.
  108. *
  109. * Permissions: It is assumed that the clock_settime() function defined
  110. * for each clock will take care of permission checks. Some
  111. * clocks may be set able by any user (i.e. local process
  112. * clocks) others not. Currently the only set able clock we
  113. * have is CLOCK_REALTIME and its high res counter part, both of
  114. * which we beg off on and pass to do_sys_settimeofday().
  115. */
  116. static struct k_clock posix_clocks[MAX_CLOCKS];
  117. /*
  118. * These ones are defined below.
  119. */
  120. static int common_nsleep(const clockid_t, int flags, struct timespec *t,
  121. struct timespec __user *rmtp);
  122. static int common_timer_create(struct k_itimer *new_timer);
  123. static void common_timer_get(struct k_itimer *, struct itimerspec *);
  124. static int common_timer_set(struct k_itimer *, int,
  125. struct itimerspec *, struct itimerspec *);
  126. static int common_timer_del(struct k_itimer *timer);
  127. static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);
  128. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
  129. #define lock_timer(tid, flags) \
  130. ({ struct k_itimer *__timr; \
  131. __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
  132. __timr; \
  133. })
  134. static int hash(struct signal_struct *sig, unsigned int nr)
  135. {
  136. return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
  137. }
  138. static struct k_itimer *__posix_timers_find(struct hlist_head *head,
  139. struct signal_struct *sig,
  140. timer_t id)
  141. {
  142. struct k_itimer *timer;
  143. hlist_for_each_entry_rcu(timer, head, t_hash) {
  144. if ((timer->it_signal == sig) && (timer->it_id == id))
  145. return timer;
  146. }
  147. return NULL;
  148. }
  149. static struct k_itimer *posix_timer_by_id(timer_t id)
  150. {
  151. struct signal_struct *sig = current->signal;
  152. struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
  153. return __posix_timers_find(head, sig, id);
  154. }
  155. static int posix_timer_add(struct k_itimer *timer)
  156. {
  157. struct signal_struct *sig = current->signal;
  158. int first_free_id = sig->posix_timer_id;
  159. struct hlist_head *head;
  160. int ret = -ENOENT;
  161. do {
  162. spin_lock(&hash_lock);
  163. head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
  164. if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
  165. hlist_add_head_rcu(&timer->t_hash, head);
  166. ret = sig->posix_timer_id;
  167. }
  168. if (++sig->posix_timer_id < 0)
  169. sig->posix_timer_id = 0;
  170. if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
  171. /* Loop over all possible ids completed */
  172. ret = -EAGAIN;
  173. spin_unlock(&hash_lock);
  174. } while (ret == -ENOENT);
  175. return ret;
  176. }
  177. static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
  178. {
  179. spin_unlock_irqrestore(&timr->it_lock, flags);
  180. }
  181. /* Get clock_realtime */
  182. static int posix_clock_realtime_get(clockid_t which_clock, struct timespec *tp)
  183. {
  184. ktime_get_real_ts(tp);
  185. return 0;
  186. }
  187. /* Set clock_realtime */
  188. static int posix_clock_realtime_set(const clockid_t which_clock,
  189. const struct timespec *tp)
  190. {
  191. return do_sys_settimeofday(tp, NULL);
  192. }
  193. static int posix_clock_realtime_adj(const clockid_t which_clock,
  194. struct timex *t)
  195. {
  196. return do_adjtimex(t);
  197. }
  198. /*
  199. * Get monotonic time for posix timers
  200. */
  201. static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
  202. {
  203. ktime_get_ts(tp);
  204. return 0;
  205. }
  206. /*
  207. * Get monotonic-raw time for posix timers
  208. */
  209. static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)
  210. {
  211. getrawmonotonic(tp);
  212. return 0;
  213. }
  214. static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec *tp)
  215. {
  216. *tp = current_kernel_time();
  217. return 0;
  218. }
  219. static int posix_get_monotonic_coarse(clockid_t which_clock,
  220. struct timespec *tp)
  221. {
  222. *tp = get_monotonic_coarse();
  223. return 0;
  224. }
  225. static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp)
  226. {
  227. *tp = ktime_to_timespec(KTIME_LOW_RES);
  228. return 0;
  229. }
  230. static int posix_get_boottime(const clockid_t which_clock, struct timespec *tp)
  231. {
  232. get_monotonic_boottime(tp);
  233. return 0;
  234. }
  235. static int posix_get_tai(clockid_t which_clock, struct timespec *tp)
  236. {
  237. timekeeping_clocktai(tp);
  238. return 0;
  239. }
  240. static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec *tp)
  241. {
  242. tp->tv_sec = 0;
  243. tp->tv_nsec = hrtimer_resolution;
  244. return 0;
  245. }
  246. /*
  247. * Initialize everything, well, just everything in Posix clocks/timers ;)
  248. */
  249. static __init int init_posix_timers(void)
  250. {
  251. struct k_clock clock_realtime = {
  252. .clock_getres = posix_get_hrtimer_res,
  253. .clock_get = posix_clock_realtime_get,
  254. .clock_set = posix_clock_realtime_set,
  255. .clock_adj = posix_clock_realtime_adj,
  256. .nsleep = common_nsleep,
  257. .nsleep_restart = hrtimer_nanosleep_restart,
  258. .timer_create = common_timer_create,
  259. .timer_set = common_timer_set,
  260. .timer_get = common_timer_get,
  261. .timer_del = common_timer_del,
  262. };
  263. struct k_clock clock_monotonic = {
  264. .clock_getres = posix_get_hrtimer_res,
  265. .clock_get = posix_ktime_get_ts,
  266. .nsleep = common_nsleep,
  267. .nsleep_restart = hrtimer_nanosleep_restart,
  268. .timer_create = common_timer_create,
  269. .timer_set = common_timer_set,
  270. .timer_get = common_timer_get,
  271. .timer_del = common_timer_del,
  272. };
  273. struct k_clock clock_monotonic_raw = {
  274. .clock_getres = posix_get_hrtimer_res,
  275. .clock_get = posix_get_monotonic_raw,
  276. };
  277. struct k_clock clock_realtime_coarse = {
  278. .clock_getres = posix_get_coarse_res,
  279. .clock_get = posix_get_realtime_coarse,
  280. };
  281. struct k_clock clock_monotonic_coarse = {
  282. .clock_getres = posix_get_coarse_res,
  283. .clock_get = posix_get_monotonic_coarse,
  284. };
  285. struct k_clock clock_tai = {
  286. .clock_getres = posix_get_hrtimer_res,
  287. .clock_get = posix_get_tai,
  288. .nsleep = common_nsleep,
  289. .nsleep_restart = hrtimer_nanosleep_restart,
  290. .timer_create = common_timer_create,
  291. .timer_set = common_timer_set,
  292. .timer_get = common_timer_get,
  293. .timer_del = common_timer_del,
  294. };
  295. struct k_clock clock_boottime = {
  296. .clock_getres = posix_get_hrtimer_res,
  297. .clock_get = posix_get_boottime,
  298. .nsleep = common_nsleep,
  299. .nsleep_restart = hrtimer_nanosleep_restart,
  300. .timer_create = common_timer_create,
  301. .timer_set = common_timer_set,
  302. .timer_get = common_timer_get,
  303. .timer_del = common_timer_del,
  304. };
  305. posix_timers_register_clock(CLOCK_REALTIME, &clock_realtime);
  306. posix_timers_register_clock(CLOCK_MONOTONIC, &clock_monotonic);
  307. posix_timers_register_clock(CLOCK_MONOTONIC_RAW, &clock_monotonic_raw);
  308. posix_timers_register_clock(CLOCK_REALTIME_COARSE, &clock_realtime_coarse);
  309. posix_timers_register_clock(CLOCK_MONOTONIC_COARSE, &clock_monotonic_coarse);
  310. posix_timers_register_clock(CLOCK_BOOTTIME, &clock_boottime);
  311. posix_timers_register_clock(CLOCK_TAI, &clock_tai);
  312. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  313. sizeof (struct k_itimer), 0, SLAB_PANIC,
  314. NULL);
  315. return 0;
  316. }
  317. __initcall(init_posix_timers);
  318. static void schedule_next_timer(struct k_itimer *timr)
  319. {
  320. struct hrtimer *timer = &timr->it.real.timer;
  321. if (timr->it.real.interval == 0)
  322. return;
  323. timr->it_overrun += (unsigned int) hrtimer_forward(timer,
  324. timer->base->get_time(),
  325. timr->it.real.interval);
  326. timr->it_overrun_last = timr->it_overrun;
  327. timr->it_overrun = -1;
  328. ++timr->it_requeue_pending;
  329. hrtimer_restart(timer);
  330. }
  331. /*
  332. * This function is exported for use by the signal deliver code. It is
  333. * called just prior to the info block being released and passes that
  334. * block to us. It's function is to update the overrun entry AND to
  335. * restart the timer. It should only be called if the timer is to be
  336. * restarted (i.e. we have flagged this in the sys_private entry of the
  337. * info block).
  338. *
  339. * To protect against the timer going away while the interrupt is queued,
  340. * we require that the it_requeue_pending flag be set.
  341. */
  342. void do_schedule_next_timer(struct siginfo *info)
  343. {
  344. struct k_itimer *timr;
  345. unsigned long flags;
  346. timr = lock_timer(info->si_tid, &flags);
  347. if (timr && timr->it_requeue_pending == info->si_sys_private) {
  348. if (timr->it_clock < 0)
  349. posix_cpu_timer_schedule(timr);
  350. else
  351. schedule_next_timer(timr);
  352. info->si_overrun += timr->it_overrun_last;
  353. }
  354. if (timr)
  355. unlock_timer(timr, flags);
  356. }
  357. int posix_timer_event(struct k_itimer *timr, int si_private)
  358. {
  359. struct task_struct *task;
  360. int shared, ret = -1;
  361. /*
  362. * FIXME: if ->sigq is queued we can race with
  363. * dequeue_signal()->do_schedule_next_timer().
  364. *
  365. * If dequeue_signal() sees the "right" value of
  366. * si_sys_private it calls do_schedule_next_timer().
  367. * We re-queue ->sigq and drop ->it_lock().
  368. * do_schedule_next_timer() locks the timer
  369. * and re-schedules it while ->sigq is pending.
  370. * Not really bad, but not that we want.
  371. */
  372. timr->sigq->info.si_sys_private = si_private;
  373. rcu_read_lock();
  374. task = pid_task(timr->it_pid, PIDTYPE_PID);
  375. if (task) {
  376. shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
  377. ret = send_sigqueue(timr->sigq, task, shared);
  378. }
  379. rcu_read_unlock();
  380. /* If we failed to send the signal the timer stops. */
  381. return ret > 0;
  382. }
  383. EXPORT_SYMBOL_GPL(posix_timer_event);
  384. /*
  385. * This function gets called when a POSIX.1b interval timer expires. It
  386. * is used as a callback from the kernel internal timer. The
  387. * run_timer_list code ALWAYS calls with interrupts on.
  388. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  389. */
  390. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  391. {
  392. struct k_itimer *timr;
  393. unsigned long flags;
  394. int si_private = 0;
  395. enum hrtimer_restart ret = HRTIMER_NORESTART;
  396. timr = container_of(timer, struct k_itimer, it.real.timer);
  397. spin_lock_irqsave(&timr->it_lock, flags);
  398. if (timr->it.real.interval != 0)
  399. si_private = ++timr->it_requeue_pending;
  400. if (posix_timer_event(timr, si_private)) {
  401. /*
  402. * signal was not sent because of sig_ignor
  403. * we will not get a call back to restart it AND
  404. * it should be restarted.
  405. */
  406. if (timr->it.real.interval != 0) {
  407. ktime_t now = hrtimer_cb_get_time(timer);
  408. /*
  409. * FIXME: What we really want, is to stop this
  410. * timer completely and restart it in case the
  411. * SIG_IGN is removed. This is a non trivial
  412. * change which involves sighand locking
  413. * (sigh !), which we don't want to do late in
  414. * the release cycle.
  415. *
  416. * For now we just let timers with an interval
  417. * less than a jiffie expire every jiffie to
  418. * avoid softirq starvation in case of SIG_IGN
  419. * and a very small interval, which would put
  420. * the timer right back on the softirq pending
  421. * list. By moving now ahead of time we trick
  422. * hrtimer_forward() to expire the timer
  423. * later, while we still maintain the overrun
  424. * accuracy, but have some inconsistency in
  425. * the timer_gettime() case. This is at least
  426. * better than a starved softirq. A more
  427. * complex fix which solves also another related
  428. * inconsistency is already in the pipeline.
  429. */
  430. #ifdef CONFIG_HIGH_RES_TIMERS
  431. {
  432. ktime_t kj = NSEC_PER_SEC / HZ;
  433. if (timr->it.real.interval < kj)
  434. now = ktime_add(now, kj);
  435. }
  436. #endif
  437. timr->it_overrun += (unsigned int)
  438. hrtimer_forward(timer, now,
  439. timr->it.real.interval);
  440. ret = HRTIMER_RESTART;
  441. ++timr->it_requeue_pending;
  442. }
  443. }
  444. unlock_timer(timr, flags);
  445. return ret;
  446. }
  447. static struct pid *good_sigevent(sigevent_t * event)
  448. {
  449. struct task_struct *rtn = current->group_leader;
  450. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  451. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  452. !same_thread_group(rtn, current) ||
  453. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  454. return NULL;
  455. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  456. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  457. return NULL;
  458. return task_pid(rtn);
  459. }
  460. void posix_timers_register_clock(const clockid_t clock_id,
  461. struct k_clock *new_clock)
  462. {
  463. if ((unsigned) clock_id >= MAX_CLOCKS) {
  464. printk(KERN_WARNING "POSIX clock register failed for clock_id %d\n",
  465. clock_id);
  466. return;
  467. }
  468. if (!new_clock->clock_get) {
  469. printk(KERN_WARNING "POSIX clock id %d lacks clock_get()\n",
  470. clock_id);
  471. return;
  472. }
  473. if (!new_clock->clock_getres) {
  474. printk(KERN_WARNING "POSIX clock id %d lacks clock_getres()\n",
  475. clock_id);
  476. return;
  477. }
  478. posix_clocks[clock_id] = *new_clock;
  479. }
  480. EXPORT_SYMBOL_GPL(posix_timers_register_clock);
  481. static struct k_itimer * alloc_posix_timer(void)
  482. {
  483. struct k_itimer *tmr;
  484. tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  485. if (!tmr)
  486. return tmr;
  487. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  488. kmem_cache_free(posix_timers_cache, tmr);
  489. return NULL;
  490. }
  491. memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
  492. return tmr;
  493. }
  494. static void k_itimer_rcu_free(struct rcu_head *head)
  495. {
  496. struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
  497. kmem_cache_free(posix_timers_cache, tmr);
  498. }
  499. #define IT_ID_SET 1
  500. #define IT_ID_NOT_SET 0
  501. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  502. {
  503. if (it_id_set) {
  504. unsigned long flags;
  505. spin_lock_irqsave(&hash_lock, flags);
  506. hlist_del_rcu(&tmr->t_hash);
  507. spin_unlock_irqrestore(&hash_lock, flags);
  508. }
  509. put_pid(tmr->it_pid);
  510. sigqueue_free(tmr->sigq);
  511. call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
  512. }
  513. static struct k_clock *clockid_to_kclock(const clockid_t id)
  514. {
  515. if (id < 0)
  516. return (id & CLOCKFD_MASK) == CLOCKFD ?
  517. &clock_posix_dynamic : &clock_posix_cpu;
  518. if (id >= MAX_CLOCKS || !posix_clocks[id].clock_getres)
  519. return NULL;
  520. return &posix_clocks[id];
  521. }
  522. static int common_timer_create(struct k_itimer *new_timer)
  523. {
  524. hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
  525. return 0;
  526. }
  527. /* Create a POSIX.1b interval timer. */
  528. SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
  529. struct sigevent __user *, timer_event_spec,
  530. timer_t __user *, created_timer_id)
  531. {
  532. struct k_clock *kc = clockid_to_kclock(which_clock);
  533. struct k_itimer *new_timer;
  534. int error, new_timer_id;
  535. sigevent_t event;
  536. int it_id_set = IT_ID_NOT_SET;
  537. if (!kc)
  538. return -EINVAL;
  539. if (!kc->timer_create)
  540. return -EOPNOTSUPP;
  541. new_timer = alloc_posix_timer();
  542. if (unlikely(!new_timer))
  543. return -EAGAIN;
  544. spin_lock_init(&new_timer->it_lock);
  545. new_timer_id = posix_timer_add(new_timer);
  546. if (new_timer_id < 0) {
  547. error = new_timer_id;
  548. goto out;
  549. }
  550. it_id_set = IT_ID_SET;
  551. new_timer->it_id = (timer_t) new_timer_id;
  552. new_timer->it_clock = which_clock;
  553. new_timer->it_overrun = -1;
  554. if (timer_event_spec) {
  555. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  556. error = -EFAULT;
  557. goto out;
  558. }
  559. rcu_read_lock();
  560. new_timer->it_pid = get_pid(good_sigevent(&event));
  561. rcu_read_unlock();
  562. if (!new_timer->it_pid) {
  563. error = -EINVAL;
  564. goto out;
  565. }
  566. } else {
  567. memset(&event.sigev_value, 0, sizeof(event.sigev_value));
  568. event.sigev_notify = SIGEV_SIGNAL;
  569. event.sigev_signo = SIGALRM;
  570. event.sigev_value.sival_int = new_timer->it_id;
  571. new_timer->it_pid = get_pid(task_tgid(current));
  572. }
  573. new_timer->it_sigev_notify = event.sigev_notify;
  574. new_timer->sigq->info.si_signo = event.sigev_signo;
  575. new_timer->sigq->info.si_value = event.sigev_value;
  576. new_timer->sigq->info.si_tid = new_timer->it_id;
  577. new_timer->sigq->info.si_code = SI_TIMER;
  578. if (copy_to_user(created_timer_id,
  579. &new_timer_id, sizeof (new_timer_id))) {
  580. error = -EFAULT;
  581. goto out;
  582. }
  583. error = kc->timer_create(new_timer);
  584. if (error)
  585. goto out;
  586. spin_lock_irq(&current->sighand->siglock);
  587. new_timer->it_signal = current->signal;
  588. list_add(&new_timer->list, &current->signal->posix_timers);
  589. spin_unlock_irq(&current->sighand->siglock);
  590. return 0;
  591. /*
  592. * In the case of the timer belonging to another task, after
  593. * the task is unlocked, the timer is owned by the other task
  594. * and may cease to exist at any time. Don't use or modify
  595. * new_timer after the unlock call.
  596. */
  597. out:
  598. release_posix_timer(new_timer, it_id_set);
  599. return error;
  600. }
  601. /*
  602. * Locking issues: We need to protect the result of the id look up until
  603. * we get the timer locked down so it is not deleted under us. The
  604. * removal is done under the idr spinlock so we use that here to bridge
  605. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  606. * be release with out holding the timer lock.
  607. */
  608. static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
  609. {
  610. struct k_itimer *timr;
  611. /*
  612. * timer_t could be any type >= int and we want to make sure any
  613. * @timer_id outside positive int range fails lookup.
  614. */
  615. if ((unsigned long long)timer_id > INT_MAX)
  616. return NULL;
  617. rcu_read_lock();
  618. timr = posix_timer_by_id(timer_id);
  619. if (timr) {
  620. spin_lock_irqsave(&timr->it_lock, *flags);
  621. if (timr->it_signal == current->signal) {
  622. rcu_read_unlock();
  623. return timr;
  624. }
  625. spin_unlock_irqrestore(&timr->it_lock, *flags);
  626. }
  627. rcu_read_unlock();
  628. return NULL;
  629. }
  630. /*
  631. * Get the time remaining on a POSIX.1b interval timer. This function
  632. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  633. * mess with irq.
  634. *
  635. * We have a couple of messes to clean up here. First there is the case
  636. * of a timer that has a requeue pending. These timers should appear to
  637. * be in the timer list with an expiry as if we were to requeue them
  638. * now.
  639. *
  640. * The second issue is the SIGEV_NONE timer which may be active but is
  641. * not really ever put in the timer list (to save system resources).
  642. * This timer may be expired, and if so, we will do it here. Otherwise
  643. * it is the same as a requeue pending timer WRT to what we should
  644. * report.
  645. */
  646. static void
  647. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  648. {
  649. ktime_t now, remaining, iv;
  650. struct hrtimer *timer = &timr->it.real.timer;
  651. memset(cur_setting, 0, sizeof(struct itimerspec));
  652. iv = timr->it.real.interval;
  653. /* interval timer ? */
  654. if (iv)
  655. cur_setting->it_interval = ktime_to_timespec(iv);
  656. else if (!hrtimer_active(timer) &&
  657. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  658. return;
  659. now = timer->base->get_time();
  660. /*
  661. * When a requeue is pending or this is a SIGEV_NONE
  662. * timer move the expiry time forward by intervals, so
  663. * expiry is > now.
  664. */
  665. if (iv && (timr->it_requeue_pending & REQUEUE_PENDING ||
  666. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  667. timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
  668. remaining = __hrtimer_expires_remaining_adjusted(timer, now);
  669. /* Return 0 only, when the timer is expired and not pending */
  670. if (remaining <= 0) {
  671. /*
  672. * A single shot SIGEV_NONE timer must return 0, when
  673. * it is expired !
  674. */
  675. if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  676. cur_setting->it_value.tv_nsec = 1;
  677. } else
  678. cur_setting->it_value = ktime_to_timespec(remaining);
  679. }
  680. /* Get the time remaining on a POSIX.1b interval timer. */
  681. SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
  682. struct itimerspec __user *, setting)
  683. {
  684. struct itimerspec cur_setting;
  685. struct k_itimer *timr;
  686. struct k_clock *kc;
  687. unsigned long flags;
  688. int ret = 0;
  689. timr = lock_timer(timer_id, &flags);
  690. if (!timr)
  691. return -EINVAL;
  692. kc = clockid_to_kclock(timr->it_clock);
  693. if (WARN_ON_ONCE(!kc || !kc->timer_get))
  694. ret = -EINVAL;
  695. else
  696. kc->timer_get(timr, &cur_setting);
  697. unlock_timer(timr, flags);
  698. if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  699. return -EFAULT;
  700. return ret;
  701. }
  702. /*
  703. * Get the number of overruns of a POSIX.1b interval timer. This is to
  704. * be the overrun of the timer last delivered. At the same time we are
  705. * accumulating overruns on the next timer. The overrun is frozen when
  706. * the signal is delivered, either at the notify time (if the info block
  707. * is not queued) or at the actual delivery time (as we are informed by
  708. * the call back to do_schedule_next_timer(). So all we need to do is
  709. * to pick up the frozen overrun.
  710. */
  711. SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  712. {
  713. struct k_itimer *timr;
  714. int overrun;
  715. unsigned long flags;
  716. timr = lock_timer(timer_id, &flags);
  717. if (!timr)
  718. return -EINVAL;
  719. overrun = timr->it_overrun_last;
  720. unlock_timer(timr, flags);
  721. return overrun;
  722. }
  723. /* Set a POSIX.1b interval timer. */
  724. /* timr->it_lock is taken. */
  725. static int
  726. common_timer_set(struct k_itimer *timr, int flags,
  727. struct itimerspec *new_setting, struct itimerspec *old_setting)
  728. {
  729. struct hrtimer *timer = &timr->it.real.timer;
  730. enum hrtimer_mode mode;
  731. if (old_setting)
  732. common_timer_get(timr, old_setting);
  733. /* disable the timer */
  734. timr->it.real.interval = 0;
  735. /*
  736. * careful here. If smp we could be in the "fire" routine which will
  737. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  738. */
  739. if (hrtimer_try_to_cancel(timer) < 0)
  740. return TIMER_RETRY;
  741. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  742. ~REQUEUE_PENDING;
  743. timr->it_overrun_last = 0;
  744. /* switch off the timer when it_value is zero */
  745. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  746. return 0;
  747. mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  748. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  749. timr->it.real.timer.function = posix_timer_fn;
  750. hrtimer_set_expires(timer, timespec_to_ktime(new_setting->it_value));
  751. /* Convert interval */
  752. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  753. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  754. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
  755. /* Setup correct expiry time for relative timers */
  756. if (mode == HRTIMER_MODE_REL) {
  757. hrtimer_add_expires(timer, timer->base->get_time());
  758. }
  759. return 0;
  760. }
  761. hrtimer_start_expires(timer, mode);
  762. return 0;
  763. }
  764. /* Set a POSIX.1b interval timer */
  765. SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  766. const struct itimerspec __user *, new_setting,
  767. struct itimerspec __user *, old_setting)
  768. {
  769. struct k_itimer *timr;
  770. struct itimerspec new_spec, old_spec;
  771. int error = 0;
  772. unsigned long flag;
  773. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  774. struct k_clock *kc;
  775. if (!new_setting)
  776. return -EINVAL;
  777. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  778. return -EFAULT;
  779. if (!timespec_valid(&new_spec.it_interval) ||
  780. !timespec_valid(&new_spec.it_value))
  781. return -EINVAL;
  782. retry:
  783. timr = lock_timer(timer_id, &flag);
  784. if (!timr)
  785. return -EINVAL;
  786. kc = clockid_to_kclock(timr->it_clock);
  787. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  788. error = -EINVAL;
  789. else
  790. error = kc->timer_set(timr, flags, &new_spec, rtn);
  791. unlock_timer(timr, flag);
  792. if (error == TIMER_RETRY) {
  793. rtn = NULL; // We already got the old time...
  794. goto retry;
  795. }
  796. if (old_setting && !error &&
  797. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  798. error = -EFAULT;
  799. return error;
  800. }
  801. static int common_timer_del(struct k_itimer *timer)
  802. {
  803. timer->it.real.interval = 0;
  804. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  805. return TIMER_RETRY;
  806. return 0;
  807. }
  808. static inline int timer_delete_hook(struct k_itimer *timer)
  809. {
  810. struct k_clock *kc = clockid_to_kclock(timer->it_clock);
  811. if (WARN_ON_ONCE(!kc || !kc->timer_del))
  812. return -EINVAL;
  813. return kc->timer_del(timer);
  814. }
  815. /* Delete a POSIX.1b interval timer. */
  816. SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  817. {
  818. struct k_itimer *timer;
  819. unsigned long flags;
  820. retry_delete:
  821. timer = lock_timer(timer_id, &flags);
  822. if (!timer)
  823. return -EINVAL;
  824. if (timer_delete_hook(timer) == TIMER_RETRY) {
  825. unlock_timer(timer, flags);
  826. goto retry_delete;
  827. }
  828. spin_lock(&current->sighand->siglock);
  829. list_del(&timer->list);
  830. spin_unlock(&current->sighand->siglock);
  831. /*
  832. * This keeps any tasks waiting on the spin lock from thinking
  833. * they got something (see the lock code above).
  834. */
  835. timer->it_signal = NULL;
  836. unlock_timer(timer, flags);
  837. release_posix_timer(timer, IT_ID_SET);
  838. return 0;
  839. }
  840. /*
  841. * return timer owned by the process, used by exit_itimers
  842. */
  843. static void itimer_delete(struct k_itimer *timer)
  844. {
  845. unsigned long flags;
  846. retry_delete:
  847. spin_lock_irqsave(&timer->it_lock, flags);
  848. if (timer_delete_hook(timer) == TIMER_RETRY) {
  849. unlock_timer(timer, flags);
  850. goto retry_delete;
  851. }
  852. list_del(&timer->list);
  853. /*
  854. * This keeps any tasks waiting on the spin lock from thinking
  855. * they got something (see the lock code above).
  856. */
  857. timer->it_signal = NULL;
  858. unlock_timer(timer, flags);
  859. release_posix_timer(timer, IT_ID_SET);
  860. }
  861. /*
  862. * This is called by do_exit or de_thread, only when there are no more
  863. * references to the shared signal_struct.
  864. */
  865. void exit_itimers(struct signal_struct *sig)
  866. {
  867. struct k_itimer *tmr;
  868. while (!list_empty(&sig->posix_timers)) {
  869. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  870. itimer_delete(tmr);
  871. }
  872. }
  873. SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
  874. const struct timespec __user *, tp)
  875. {
  876. struct k_clock *kc = clockid_to_kclock(which_clock);
  877. struct timespec new_tp;
  878. if (!kc || !kc->clock_set)
  879. return -EINVAL;
  880. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  881. return -EFAULT;
  882. return kc->clock_set(which_clock, &new_tp);
  883. }
  884. SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
  885. struct timespec __user *,tp)
  886. {
  887. struct k_clock *kc = clockid_to_kclock(which_clock);
  888. struct timespec kernel_tp;
  889. int error;
  890. if (!kc)
  891. return -EINVAL;
  892. error = kc->clock_get(which_clock, &kernel_tp);
  893. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  894. error = -EFAULT;
  895. return error;
  896. }
  897. SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
  898. struct timex __user *, utx)
  899. {
  900. struct k_clock *kc = clockid_to_kclock(which_clock);
  901. struct timex ktx;
  902. int err;
  903. if (!kc)
  904. return -EINVAL;
  905. if (!kc->clock_adj)
  906. return -EOPNOTSUPP;
  907. if (copy_from_user(&ktx, utx, sizeof(ktx)))
  908. return -EFAULT;
  909. err = kc->clock_adj(which_clock, &ktx);
  910. if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
  911. return -EFAULT;
  912. return err;
  913. }
  914. SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
  915. struct timespec __user *, tp)
  916. {
  917. struct k_clock *kc = clockid_to_kclock(which_clock);
  918. struct timespec rtn_tp;
  919. int error;
  920. if (!kc)
  921. return -EINVAL;
  922. error = kc->clock_getres(which_clock, &rtn_tp);
  923. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
  924. error = -EFAULT;
  925. return error;
  926. }
  927. /*
  928. * nanosleep for monotonic and realtime clocks
  929. */
  930. static int common_nsleep(const clockid_t which_clock, int flags,
  931. struct timespec *tsave, struct timespec __user *rmtp)
  932. {
  933. return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
  934. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  935. which_clock);
  936. }
  937. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  938. const struct timespec __user *, rqtp,
  939. struct timespec __user *, rmtp)
  940. {
  941. struct k_clock *kc = clockid_to_kclock(which_clock);
  942. struct timespec t;
  943. if (!kc)
  944. return -EINVAL;
  945. if (!kc->nsleep)
  946. return -ENANOSLEEP_NOTSUP;
  947. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  948. return -EFAULT;
  949. if (!timespec_valid(&t))
  950. return -EINVAL;
  951. return kc->nsleep(which_clock, flags, &t, rmtp);
  952. }
  953. /*
  954. * This will restart clock_nanosleep. This is required only by
  955. * compat_clock_nanosleep_restart for now.
  956. */
  957. long clock_nanosleep_restart(struct restart_block *restart_block)
  958. {
  959. clockid_t which_clock = restart_block->nanosleep.clockid;
  960. struct k_clock *kc = clockid_to_kclock(which_clock);
  961. if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
  962. return -EINVAL;
  963. return kc->nsleep_restart(restart_block);
  964. }