posix-timers.c 29 KB

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