posix-timers.c 30 KB

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