posix-timers.c 30 KB

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