posix-timers.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. memset(&timr->sigq->info, 0, sizeof(siginfo_t));
  266. timr->sigq->info.si_sys_private = si_private;
  267. /* Send signal to the process that owns this timer.*/
  268. timr->sigq->info.si_signo = timr->it_sigev_signo;
  269. timr->sigq->info.si_errno = 0;
  270. timr->sigq->info.si_code = SI_TIMER;
  271. timr->sigq->info.si_tid = timr->it_id;
  272. timr->sigq->info.si_value = timr->it_sigev_value;
  273. if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
  274. struct task_struct *leader;
  275. int ret = send_sigqueue(timr->sigq, timr->it_process, 0);
  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_sigqueue(timr->sigq, timr->it_process, 1);
  284. }
  285. EXPORT_SYMBOL_GPL(posix_timer_event);
  286. /*
  287. * This function gets called when a POSIX.1b interval timer expires. It
  288. * is used as a callback from the kernel internal timer. The
  289. * run_timer_list code ALWAYS calls with interrupts on.
  290. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
  291. */
  292. static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  293. {
  294. struct k_itimer *timr;
  295. unsigned long flags;
  296. int si_private = 0;
  297. enum hrtimer_restart ret = HRTIMER_NORESTART;
  298. timr = container_of(timer, struct k_itimer, it.real.timer);
  299. spin_lock_irqsave(&timr->it_lock, flags);
  300. if (timr->it.real.interval.tv64 != 0)
  301. si_private = ++timr->it_requeue_pending;
  302. if (posix_timer_event(timr, si_private)) {
  303. /*
  304. * signal was not sent because of sig_ignor
  305. * we will not get a call back to restart it AND
  306. * it should be restarted.
  307. */
  308. if (timr->it.real.interval.tv64 != 0) {
  309. ktime_t now = hrtimer_cb_get_time(timer);
  310. /*
  311. * FIXME: What we really want, is to stop this
  312. * timer completely and restart it in case the
  313. * SIG_IGN is removed. This is a non trivial
  314. * change which involves sighand locking
  315. * (sigh !), which we don't want to do late in
  316. * the release cycle.
  317. *
  318. * For now we just let timers with an interval
  319. * less than a jiffie expire every jiffie to
  320. * avoid softirq starvation in case of SIG_IGN
  321. * and a very small interval, which would put
  322. * the timer right back on the softirq pending
  323. * list. By moving now ahead of time we trick
  324. * hrtimer_forward() to expire the timer
  325. * later, while we still maintain the overrun
  326. * accuracy, but have some inconsistency in
  327. * the timer_gettime() case. This is at least
  328. * better than a starved softirq. A more
  329. * complex fix which solves also another related
  330. * inconsistency is already in the pipeline.
  331. */
  332. #ifdef CONFIG_HIGH_RES_TIMERS
  333. {
  334. ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ);
  335. if (timr->it.real.interval.tv64 < kj.tv64)
  336. now = ktime_add(now, kj);
  337. }
  338. #endif
  339. timr->it_overrun += (unsigned int)
  340. hrtimer_forward(timer, now,
  341. timr->it.real.interval);
  342. ret = HRTIMER_RESTART;
  343. ++timr->it_requeue_pending;
  344. }
  345. }
  346. unlock_timer(timr, flags);
  347. return ret;
  348. }
  349. static struct task_struct * good_sigevent(sigevent_t * event)
  350. {
  351. struct task_struct *rtn = current->group_leader;
  352. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  353. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  354. !same_thread_group(rtn, current) ||
  355. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  356. return NULL;
  357. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  358. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  359. return NULL;
  360. return rtn;
  361. }
  362. void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock)
  363. {
  364. if ((unsigned) clock_id >= MAX_CLOCKS) {
  365. printk("POSIX clock register failed for clock_id %d\n",
  366. clock_id);
  367. return;
  368. }
  369. posix_clocks[clock_id] = *new_clock;
  370. }
  371. EXPORT_SYMBOL_GPL(register_posix_clock);
  372. static struct k_itimer * alloc_posix_timer(void)
  373. {
  374. struct k_itimer *tmr;
  375. tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
  376. if (!tmr)
  377. return tmr;
  378. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  379. kmem_cache_free(posix_timers_cache, tmr);
  380. tmr = NULL;
  381. }
  382. return tmr;
  383. }
  384. #define IT_ID_SET 1
  385. #define IT_ID_NOT_SET 0
  386. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  387. {
  388. if (it_id_set) {
  389. unsigned long flags;
  390. spin_lock_irqsave(&idr_lock, flags);
  391. idr_remove(&posix_timers_id, tmr->it_id);
  392. spin_unlock_irqrestore(&idr_lock, flags);
  393. }
  394. sigqueue_free(tmr->sigq);
  395. kmem_cache_free(posix_timers_cache, tmr);
  396. }
  397. /* Create a POSIX.1b interval timer. */
  398. asmlinkage long
  399. sys_timer_create(const clockid_t which_clock,
  400. struct sigevent __user *timer_event_spec,
  401. timer_t __user * created_timer_id)
  402. {
  403. int error = 0;
  404. struct k_itimer *new_timer = NULL;
  405. int new_timer_id;
  406. struct task_struct *process = NULL;
  407. unsigned long flags;
  408. sigevent_t event;
  409. int it_id_set = IT_ID_NOT_SET;
  410. if (invalid_clockid(which_clock))
  411. return -EINVAL;
  412. new_timer = alloc_posix_timer();
  413. if (unlikely(!new_timer))
  414. return -EAGAIN;
  415. spin_lock_init(&new_timer->it_lock);
  416. retry:
  417. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  418. error = -EAGAIN;
  419. goto out;
  420. }
  421. spin_lock_irq(&idr_lock);
  422. error = idr_get_new(&posix_timers_id, (void *) new_timer,
  423. &new_timer_id);
  424. spin_unlock_irq(&idr_lock);
  425. if (error == -EAGAIN)
  426. goto retry;
  427. else if (error) {
  428. /*
  429. * Weird looking, but we return EAGAIN if the IDR is
  430. * full (proper POSIX return value for this)
  431. */
  432. error = -EAGAIN;
  433. goto out;
  434. }
  435. it_id_set = IT_ID_SET;
  436. new_timer->it_id = (timer_t) new_timer_id;
  437. new_timer->it_clock = which_clock;
  438. new_timer->it_overrun = -1;
  439. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  440. if (error)
  441. goto out;
  442. /*
  443. * return the timer_id now. The next step is hard to
  444. * back out if there is an error.
  445. */
  446. if (copy_to_user(created_timer_id,
  447. &new_timer_id, sizeof (new_timer_id))) {
  448. error = -EFAULT;
  449. goto out;
  450. }
  451. if (timer_event_spec) {
  452. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  453. error = -EFAULT;
  454. goto out;
  455. }
  456. new_timer->it_sigev_notify = event.sigev_notify;
  457. new_timer->it_sigev_signo = event.sigev_signo;
  458. new_timer->it_sigev_value = event.sigev_value;
  459. read_lock(&tasklist_lock);
  460. if ((process = good_sigevent(&event))) {
  461. /*
  462. * We may be setting up this process for another
  463. * thread. It may be exiting. To catch this
  464. * case the we check the PF_EXITING flag. If
  465. * the flag is not set, the siglock will catch
  466. * him before it is too late (in exit_itimers).
  467. *
  468. * The exec case is a bit more invloved but easy
  469. * to code. If the process is in our thread
  470. * group (and it must be or we would not allow
  471. * it here) and is doing an exec, it will cause
  472. * us to be killed. In this case it will wait
  473. * for us to die which means we can finish this
  474. * linkage with our last gasp. I.e. no code :)
  475. */
  476. spin_lock_irqsave(&process->sighand->siglock, flags);
  477. if (!(process->flags & PF_EXITING)) {
  478. new_timer->it_process = process;
  479. list_add(&new_timer->list,
  480. &process->signal->posix_timers);
  481. if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  482. get_task_struct(process);
  483. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  484. } else {
  485. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  486. process = NULL;
  487. }
  488. }
  489. read_unlock(&tasklist_lock);
  490. if (!process) {
  491. error = -EINVAL;
  492. goto out;
  493. }
  494. } else {
  495. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  496. new_timer->it_sigev_signo = SIGALRM;
  497. new_timer->it_sigev_value.sival_int = new_timer->it_id;
  498. process = current->group_leader;
  499. spin_lock_irqsave(&process->sighand->siglock, flags);
  500. new_timer->it_process = process;
  501. list_add(&new_timer->list, &process->signal->posix_timers);
  502. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  503. }
  504. /*
  505. * In the case of the timer belonging to another task, after
  506. * the task is unlocked, the timer is owned by the other task
  507. * and may cease to exist at any time. Don't use or modify
  508. * new_timer after the unlock call.
  509. */
  510. out:
  511. if (error)
  512. release_posix_timer(new_timer, it_id_set);
  513. return error;
  514. }
  515. /*
  516. * Locking issues: We need to protect the result of the id look up until
  517. * we get the timer locked down so it is not deleted under us. The
  518. * removal is done under the idr spinlock so we use that here to bridge
  519. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  520. * be release with out holding the timer lock.
  521. */
  522. static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
  523. {
  524. struct k_itimer *timr;
  525. /*
  526. * Watch out here. We do a irqsave on the idr_lock and pass the
  527. * flags part over to the timer lock. Must not let interrupts in
  528. * while we are moving the lock.
  529. */
  530. spin_lock_irqsave(&idr_lock, *flags);
  531. timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
  532. if (timr) {
  533. spin_lock(&timr->it_lock);
  534. if ((timr->it_id != timer_id) || !(timr->it_process) ||
  535. !same_thread_group(timr->it_process, current)) {
  536. spin_unlock(&timr->it_lock);
  537. spin_unlock_irqrestore(&idr_lock, *flags);
  538. timr = NULL;
  539. } else
  540. spin_unlock(&idr_lock);
  541. } else
  542. spin_unlock_irqrestore(&idr_lock, *flags);
  543. return timr;
  544. }
  545. /*
  546. * Get the time remaining on a POSIX.1b interval timer. This function
  547. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  548. * mess with irq.
  549. *
  550. * We have a couple of messes to clean up here. First there is the case
  551. * of a timer that has a requeue pending. These timers should appear to
  552. * be in the timer list with an expiry as if we were to requeue them
  553. * now.
  554. *
  555. * The second issue is the SIGEV_NONE timer which may be active but is
  556. * not really ever put in the timer list (to save system resources).
  557. * This timer may be expired, and if so, we will do it here. Otherwise
  558. * it is the same as a requeue pending timer WRT to what we should
  559. * report.
  560. */
  561. static void
  562. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  563. {
  564. ktime_t now, remaining, iv;
  565. struct hrtimer *timer = &timr->it.real.timer;
  566. memset(cur_setting, 0, sizeof(struct itimerspec));
  567. iv = timr->it.real.interval;
  568. /* interval timer ? */
  569. if (iv.tv64)
  570. cur_setting->it_interval = ktime_to_timespec(iv);
  571. else if (!hrtimer_active(timer) &&
  572. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  573. return;
  574. now = timer->base->get_time();
  575. /*
  576. * When a requeue is pending or this is a SIGEV_NONE
  577. * timer move the expiry time forward by intervals, so
  578. * expiry is > now.
  579. */
  580. if (iv.tv64 && (timr->it_requeue_pending & REQUEUE_PENDING ||
  581. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  582. timr->it_overrun += (unsigned int) hrtimer_forward(timer, now, iv);
  583. remaining = ktime_sub(timer->expires, now);
  584. /* Return 0 only, when the timer is expired and not pending */
  585. if (remaining.tv64 <= 0) {
  586. /*
  587. * A single shot SIGEV_NONE timer must return 0, when
  588. * it is expired !
  589. */
  590. if ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE)
  591. cur_setting->it_value.tv_nsec = 1;
  592. } else
  593. cur_setting->it_value = ktime_to_timespec(remaining);
  594. }
  595. /* Get the time remaining on a POSIX.1b interval timer. */
  596. asmlinkage long
  597. sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
  598. {
  599. struct k_itimer *timr;
  600. struct itimerspec cur_setting;
  601. unsigned long flags;
  602. timr = lock_timer(timer_id, &flags);
  603. if (!timr)
  604. return -EINVAL;
  605. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  606. unlock_timer(timr, flags);
  607. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  608. return -EFAULT;
  609. return 0;
  610. }
  611. /*
  612. * Get the number of overruns of a POSIX.1b interval timer. This is to
  613. * be the overrun of the timer last delivered. At the same time we are
  614. * accumulating overruns on the next timer. The overrun is frozen when
  615. * the signal is delivered, either at the notify time (if the info block
  616. * is not queued) or at the actual delivery time (as we are informed by
  617. * the call back to do_schedule_next_timer(). So all we need to do is
  618. * to pick up the frozen overrun.
  619. */
  620. asmlinkage long
  621. sys_timer_getoverrun(timer_t timer_id)
  622. {
  623. struct k_itimer *timr;
  624. int overrun;
  625. unsigned long flags;
  626. timr = lock_timer(timer_id, &flags);
  627. if (!timr)
  628. return -EINVAL;
  629. overrun = timr->it_overrun_last;
  630. unlock_timer(timr, flags);
  631. return overrun;
  632. }
  633. /* Set a POSIX.1b interval timer. */
  634. /* timr->it_lock is taken. */
  635. static int
  636. common_timer_set(struct k_itimer *timr, int flags,
  637. struct itimerspec *new_setting, struct itimerspec *old_setting)
  638. {
  639. struct hrtimer *timer = &timr->it.real.timer;
  640. enum hrtimer_mode mode;
  641. if (old_setting)
  642. common_timer_get(timr, old_setting);
  643. /* disable the timer */
  644. timr->it.real.interval.tv64 = 0;
  645. /*
  646. * careful here. If smp we could be in the "fire" routine which will
  647. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  648. */
  649. if (hrtimer_try_to_cancel(timer) < 0)
  650. return TIMER_RETRY;
  651. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  652. ~REQUEUE_PENDING;
  653. timr->it_overrun_last = 0;
  654. /* switch off the timer when it_value is zero */
  655. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  656. return 0;
  657. mode = flags & TIMER_ABSTIME ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
  658. hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
  659. timr->it.real.timer.function = posix_timer_fn;
  660. timer->expires = timespec_to_ktime(new_setting->it_value);
  661. /* Convert interval */
  662. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  663. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  664. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
  665. /* Setup correct expiry time for relative timers */
  666. if (mode == HRTIMER_MODE_REL) {
  667. timer->expires =
  668. ktime_add_safe(timer->expires,
  669. timer->base->get_time());
  670. }
  671. return 0;
  672. }
  673. hrtimer_start(timer, timer->expires, mode);
  674. return 0;
  675. }
  676. /* Set a POSIX.1b interval timer */
  677. asmlinkage long
  678. sys_timer_settime(timer_t timer_id, int flags,
  679. const struct itimerspec __user *new_setting,
  680. struct itimerspec __user *old_setting)
  681. {
  682. struct k_itimer *timr;
  683. struct itimerspec new_spec, old_spec;
  684. int error = 0;
  685. unsigned long flag;
  686. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  687. if (!new_setting)
  688. return -EINVAL;
  689. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  690. return -EFAULT;
  691. if (!timespec_valid(&new_spec.it_interval) ||
  692. !timespec_valid(&new_spec.it_value))
  693. return -EINVAL;
  694. retry:
  695. timr = lock_timer(timer_id, &flag);
  696. if (!timr)
  697. return -EINVAL;
  698. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  699. (timr, flags, &new_spec, rtn));
  700. unlock_timer(timr, flag);
  701. if (error == TIMER_RETRY) {
  702. rtn = NULL; // We already got the old time...
  703. goto retry;
  704. }
  705. if (old_setting && !error &&
  706. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  707. error = -EFAULT;
  708. return error;
  709. }
  710. static inline int common_timer_del(struct k_itimer *timer)
  711. {
  712. timer->it.real.interval.tv64 = 0;
  713. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  714. return TIMER_RETRY;
  715. return 0;
  716. }
  717. static inline int timer_delete_hook(struct k_itimer *timer)
  718. {
  719. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  720. }
  721. /* Delete a POSIX.1b interval timer. */
  722. asmlinkage long
  723. sys_timer_delete(timer_t timer_id)
  724. {
  725. struct k_itimer *timer;
  726. unsigned long flags;
  727. retry_delete:
  728. timer = lock_timer(timer_id, &flags);
  729. if (!timer)
  730. return -EINVAL;
  731. if (timer_delete_hook(timer) == TIMER_RETRY) {
  732. unlock_timer(timer, flags);
  733. goto retry_delete;
  734. }
  735. spin_lock(&current->sighand->siglock);
  736. list_del(&timer->list);
  737. spin_unlock(&current->sighand->siglock);
  738. /*
  739. * This keeps any tasks waiting on the spin lock from thinking
  740. * they got something (see the lock code above).
  741. */
  742. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  743. put_task_struct(timer->it_process);
  744. timer->it_process = NULL;
  745. unlock_timer(timer, flags);
  746. release_posix_timer(timer, IT_ID_SET);
  747. return 0;
  748. }
  749. /*
  750. * return timer owned by the process, used by exit_itimers
  751. */
  752. static void itimer_delete(struct k_itimer *timer)
  753. {
  754. unsigned long flags;
  755. retry_delete:
  756. spin_lock_irqsave(&timer->it_lock, flags);
  757. if (timer_delete_hook(timer) == TIMER_RETRY) {
  758. unlock_timer(timer, flags);
  759. goto retry_delete;
  760. }
  761. list_del(&timer->list);
  762. /*
  763. * This keeps any tasks waiting on the spin lock from thinking
  764. * they got something (see the lock code above).
  765. */
  766. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  767. put_task_struct(timer->it_process);
  768. timer->it_process = NULL;
  769. unlock_timer(timer, flags);
  770. release_posix_timer(timer, IT_ID_SET);
  771. }
  772. /*
  773. * This is called by do_exit or de_thread, only when there are no more
  774. * references to the shared signal_struct.
  775. */
  776. void exit_itimers(struct signal_struct *sig)
  777. {
  778. struct k_itimer *tmr;
  779. while (!list_empty(&sig->posix_timers)) {
  780. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  781. itimer_delete(tmr);
  782. }
  783. }
  784. /* Not available / possible... functions */
  785. int do_posix_clock_nosettime(const clockid_t clockid, struct timespec *tp)
  786. {
  787. return -EINVAL;
  788. }
  789. EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
  790. int do_posix_clock_nonanosleep(const clockid_t clock, int flags,
  791. struct timespec *t, struct timespec __user *r)
  792. {
  793. #ifndef ENOTSUP
  794. return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
  795. #else /* parisc does define it separately. */
  796. return -ENOTSUP;
  797. #endif
  798. }
  799. EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
  800. asmlinkage long sys_clock_settime(const clockid_t which_clock,
  801. const struct timespec __user *tp)
  802. {
  803. struct timespec new_tp;
  804. if (invalid_clockid(which_clock))
  805. return -EINVAL;
  806. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  807. return -EFAULT;
  808. return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
  809. }
  810. asmlinkage long
  811. sys_clock_gettime(const clockid_t which_clock, struct timespec __user *tp)
  812. {
  813. struct timespec kernel_tp;
  814. int error;
  815. if (invalid_clockid(which_clock))
  816. return -EINVAL;
  817. error = CLOCK_DISPATCH(which_clock, clock_get,
  818. (which_clock, &kernel_tp));
  819. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  820. error = -EFAULT;
  821. return error;
  822. }
  823. asmlinkage long
  824. sys_clock_getres(const clockid_t which_clock, struct timespec __user *tp)
  825. {
  826. struct timespec rtn_tp;
  827. int error;
  828. if (invalid_clockid(which_clock))
  829. return -EINVAL;
  830. error = CLOCK_DISPATCH(which_clock, clock_getres,
  831. (which_clock, &rtn_tp));
  832. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  833. error = -EFAULT;
  834. }
  835. return error;
  836. }
  837. /*
  838. * nanosleep for monotonic and realtime clocks
  839. */
  840. static int common_nsleep(const clockid_t which_clock, int flags,
  841. struct timespec *tsave, struct timespec __user *rmtp)
  842. {
  843. return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
  844. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  845. which_clock);
  846. }
  847. asmlinkage long
  848. sys_clock_nanosleep(const clockid_t which_clock, int flags,
  849. const struct timespec __user *rqtp,
  850. struct timespec __user *rmtp)
  851. {
  852. struct timespec t;
  853. if (invalid_clockid(which_clock))
  854. return -EINVAL;
  855. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  856. return -EFAULT;
  857. if (!timespec_valid(&t))
  858. return -EINVAL;
  859. return CLOCK_DISPATCH(which_clock, nsleep,
  860. (which_clock, flags, &t, rmtp));
  861. }
  862. /*
  863. * nanosleep_restart for monotonic and realtime clocks
  864. */
  865. static int common_nsleep_restart(struct restart_block *restart_block)
  866. {
  867. return hrtimer_nanosleep_restart(restart_block);
  868. }
  869. /*
  870. * This will restart clock_nanosleep. This is required only by
  871. * compat_clock_nanosleep_restart for now.
  872. */
  873. long
  874. clock_nanosleep_restart(struct restart_block *restart_block)
  875. {
  876. clockid_t which_clock = restart_block->arg0;
  877. return CLOCK_DISPATCH(which_clock, nsleep_restart,
  878. (restart_block));
  879. }