posix-timers.c 29 KB

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