posix-timers.c 29 KB

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