posix-timers.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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/smp_lock.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/slab.h>
  35. #include <linux/time.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 kmem_cache_t *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 int posix_timer_fn(void *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);
  178. new_timer->it.real.timer.data = new_timer;
  179. new_timer->it.real.timer.function = posix_timer_fn;
  180. return 0;
  181. }
  182. /*
  183. * Return nonzero if we know a priori this clockid_t value is bogus.
  184. */
  185. static inline int invalid_clockid(const clockid_t which_clock)
  186. {
  187. if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */
  188. return 0;
  189. if ((unsigned) which_clock >= MAX_CLOCKS)
  190. return 1;
  191. if (posix_clocks[which_clock].clock_getres != NULL)
  192. return 0;
  193. if (posix_clocks[which_clock].res != 0)
  194. return 0;
  195. return 1;
  196. }
  197. /*
  198. * Get monotonic time for posix timers
  199. */
  200. static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp)
  201. {
  202. ktime_get_ts(tp);
  203. return 0;
  204. }
  205. /*
  206. * Initialize everything, well, just everything in Posix clocks/timers ;)
  207. */
  208. static __init int init_posix_timers(void)
  209. {
  210. struct k_clock clock_realtime = {
  211. .clock_getres = hrtimer_get_res,
  212. };
  213. struct k_clock clock_monotonic = {
  214. .clock_getres = hrtimer_get_res,
  215. .clock_get = posix_ktime_get_ts,
  216. .clock_set = do_posix_clock_nosettime,
  217. };
  218. register_posix_clock(CLOCK_REALTIME, &clock_realtime);
  219. register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
  220. posix_timers_cache = kmem_cache_create("posix_timers_cache",
  221. sizeof (struct k_itimer), 0, 0, NULL, NULL);
  222. idr_init(&posix_timers_id);
  223. return 0;
  224. }
  225. __initcall(init_posix_timers);
  226. static void schedule_next_timer(struct k_itimer *timr)
  227. {
  228. if (timr->it.real.interval.tv64 == 0)
  229. return;
  230. timr->it_overrun += hrtimer_forward(&timr->it.real.timer,
  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(&timr->it.real.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. 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 int posix_timer_fn(void *data)
  294. {
  295. struct k_itimer *timr = data;
  296. unsigned long flags;
  297. int si_private = 0;
  298. int ret = HRTIMER_NORESTART;
  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. timr->it_overrun +=
  310. hrtimer_forward(&timr->it.real.timer,
  311. timr->it.real.interval);
  312. ret = HRTIMER_RESTART;
  313. }
  314. }
  315. unlock_timer(timr, flags);
  316. return ret;
  317. }
  318. static struct task_struct * good_sigevent(sigevent_t * event)
  319. {
  320. struct task_struct *rtn = current->group_leader;
  321. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  322. (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
  323. rtn->tgid != current->tgid ||
  324. (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
  325. return NULL;
  326. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  327. ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  328. return NULL;
  329. return rtn;
  330. }
  331. void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock)
  332. {
  333. if ((unsigned) clock_id >= MAX_CLOCKS) {
  334. printk("POSIX clock register failed for clock_id %d\n",
  335. clock_id);
  336. return;
  337. }
  338. posix_clocks[clock_id] = *new_clock;
  339. }
  340. EXPORT_SYMBOL_GPL(register_posix_clock);
  341. static struct k_itimer * alloc_posix_timer(void)
  342. {
  343. struct k_itimer *tmr;
  344. tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
  345. if (!tmr)
  346. return tmr;
  347. memset(tmr, 0, sizeof (struct k_itimer));
  348. if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
  349. kmem_cache_free(posix_timers_cache, tmr);
  350. tmr = NULL;
  351. }
  352. return tmr;
  353. }
  354. #define IT_ID_SET 1
  355. #define IT_ID_NOT_SET 0
  356. static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
  357. {
  358. if (it_id_set) {
  359. unsigned long flags;
  360. spin_lock_irqsave(&idr_lock, flags);
  361. idr_remove(&posix_timers_id, tmr->it_id);
  362. spin_unlock_irqrestore(&idr_lock, flags);
  363. }
  364. sigqueue_free(tmr->sigq);
  365. if (unlikely(tmr->it_process) &&
  366. tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  367. put_task_struct(tmr->it_process);
  368. kmem_cache_free(posix_timers_cache, tmr);
  369. }
  370. /* Create a POSIX.1b interval timer. */
  371. asmlinkage long
  372. sys_timer_create(const clockid_t which_clock,
  373. struct sigevent __user *timer_event_spec,
  374. timer_t __user * created_timer_id)
  375. {
  376. int error = 0;
  377. struct k_itimer *new_timer = NULL;
  378. int new_timer_id;
  379. struct task_struct *process = NULL;
  380. unsigned long flags;
  381. sigevent_t event;
  382. int it_id_set = IT_ID_NOT_SET;
  383. if (invalid_clockid(which_clock))
  384. return -EINVAL;
  385. new_timer = alloc_posix_timer();
  386. if (unlikely(!new_timer))
  387. return -EAGAIN;
  388. spin_lock_init(&new_timer->it_lock);
  389. retry:
  390. if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
  391. error = -EAGAIN;
  392. goto out;
  393. }
  394. spin_lock_irq(&idr_lock);
  395. error = idr_get_new(&posix_timers_id, (void *) new_timer,
  396. &new_timer_id);
  397. spin_unlock_irq(&idr_lock);
  398. if (error == -EAGAIN)
  399. goto retry;
  400. else if (error) {
  401. /*
  402. * Wierd looking, but we return EAGAIN if the IDR is
  403. * full (proper POSIX return value for this)
  404. */
  405. error = -EAGAIN;
  406. goto out;
  407. }
  408. it_id_set = IT_ID_SET;
  409. new_timer->it_id = (timer_t) new_timer_id;
  410. new_timer->it_clock = which_clock;
  411. new_timer->it_overrun = -1;
  412. error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
  413. if (error)
  414. goto out;
  415. /*
  416. * return the timer_id now. The next step is hard to
  417. * back out if there is an error.
  418. */
  419. if (copy_to_user(created_timer_id,
  420. &new_timer_id, sizeof (new_timer_id))) {
  421. error = -EFAULT;
  422. goto out;
  423. }
  424. if (timer_event_spec) {
  425. if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
  426. error = -EFAULT;
  427. goto out;
  428. }
  429. new_timer->it_sigev_notify = event.sigev_notify;
  430. new_timer->it_sigev_signo = event.sigev_signo;
  431. new_timer->it_sigev_value = event.sigev_value;
  432. read_lock(&tasklist_lock);
  433. if ((process = good_sigevent(&event))) {
  434. /*
  435. * We may be setting up this process for another
  436. * thread. It may be exiting. To catch this
  437. * case the we check the PF_EXITING flag. If
  438. * the flag is not set, the siglock will catch
  439. * him before it is too late (in exit_itimers).
  440. *
  441. * The exec case is a bit more invloved but easy
  442. * to code. If the process is in our thread
  443. * group (and it must be or we would not allow
  444. * it here) and is doing an exec, it will cause
  445. * us to be killed. In this case it will wait
  446. * for us to die which means we can finish this
  447. * linkage with our last gasp. I.e. no code :)
  448. */
  449. spin_lock_irqsave(&process->sighand->siglock, flags);
  450. if (!(process->flags & PF_EXITING)) {
  451. new_timer->it_process = process;
  452. list_add(&new_timer->list,
  453. &process->signal->posix_timers);
  454. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  455. if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  456. get_task_struct(process);
  457. } else {
  458. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  459. process = NULL;
  460. }
  461. }
  462. read_unlock(&tasklist_lock);
  463. if (!process) {
  464. error = -EINVAL;
  465. goto out;
  466. }
  467. } else {
  468. new_timer->it_sigev_notify = SIGEV_SIGNAL;
  469. new_timer->it_sigev_signo = SIGALRM;
  470. new_timer->it_sigev_value.sival_int = new_timer->it_id;
  471. process = current->group_leader;
  472. spin_lock_irqsave(&process->sighand->siglock, flags);
  473. new_timer->it_process = process;
  474. list_add(&new_timer->list, &process->signal->posix_timers);
  475. spin_unlock_irqrestore(&process->sighand->siglock, flags);
  476. }
  477. /*
  478. * In the case of the timer belonging to another task, after
  479. * the task is unlocked, the timer is owned by the other task
  480. * and may cease to exist at any time. Don't use or modify
  481. * new_timer after the unlock call.
  482. */
  483. out:
  484. if (error)
  485. release_posix_timer(new_timer, it_id_set);
  486. return error;
  487. }
  488. /*
  489. * Locking issues: We need to protect the result of the id look up until
  490. * we get the timer locked down so it is not deleted under us. The
  491. * removal is done under the idr spinlock so we use that here to bridge
  492. * the find to the timer lock. To avoid a dead lock, the timer id MUST
  493. * be release with out holding the timer lock.
  494. */
  495. static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
  496. {
  497. struct k_itimer *timr;
  498. /*
  499. * Watch out here. We do a irqsave on the idr_lock and pass the
  500. * flags part over to the timer lock. Must not let interrupts in
  501. * while we are moving the lock.
  502. */
  503. spin_lock_irqsave(&idr_lock, *flags);
  504. timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
  505. if (timr) {
  506. spin_lock(&timr->it_lock);
  507. spin_unlock(&idr_lock);
  508. if ((timr->it_id != timer_id) || !(timr->it_process) ||
  509. timr->it_process->tgid != current->tgid) {
  510. unlock_timer(timr, *flags);
  511. timr = NULL;
  512. }
  513. } else
  514. spin_unlock_irqrestore(&idr_lock, *flags);
  515. return timr;
  516. }
  517. /*
  518. * Get the time remaining on a POSIX.1b interval timer. This function
  519. * is ALWAYS called with spin_lock_irq on the timer, thus it must not
  520. * mess with irq.
  521. *
  522. * We have a couple of messes to clean up here. First there is the case
  523. * of a timer that has a requeue pending. These timers should appear to
  524. * be in the timer list with an expiry as if we were to requeue them
  525. * now.
  526. *
  527. * The second issue is the SIGEV_NONE timer which may be active but is
  528. * not really ever put in the timer list (to save system resources).
  529. * This timer may be expired, and if so, we will do it here. Otherwise
  530. * it is the same as a requeue pending timer WRT to what we should
  531. * report.
  532. */
  533. static void
  534. common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  535. {
  536. ktime_t remaining;
  537. struct hrtimer *timer = &timr->it.real.timer;
  538. memset(cur_setting, 0, sizeof(struct itimerspec));
  539. remaining = hrtimer_get_remaining(timer);
  540. /* Time left ? or timer pending */
  541. if (remaining.tv64 > 0 || hrtimer_active(timer))
  542. goto calci;
  543. /* interval timer ? */
  544. if (timr->it.real.interval.tv64 == 0)
  545. return;
  546. /*
  547. * When a requeue is pending or this is a SIGEV_NONE timer
  548. * move the expiry time forward by intervals, so expiry is >
  549. * now.
  550. */
  551. if (timr->it_requeue_pending & REQUEUE_PENDING ||
  552. (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
  553. timr->it_overrun +=
  554. hrtimer_forward(timer, timr->it.real.interval);
  555. remaining = hrtimer_get_remaining(timer);
  556. }
  557. calci:
  558. /* interval timer ? */
  559. if (timr->it.real.interval.tv64 != 0)
  560. cur_setting->it_interval =
  561. ktime_to_timespec(timr->it.real.interval);
  562. /* Return 0 only, when the timer is expired and not pending */
  563. if (remaining.tv64 <= 0)
  564. cur_setting->it_value.tv_nsec = 1;
  565. else
  566. cur_setting->it_value = ktime_to_timespec(remaining);
  567. }
  568. /* Get the time remaining on a POSIX.1b interval timer. */
  569. asmlinkage long
  570. sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
  571. {
  572. struct k_itimer *timr;
  573. struct itimerspec cur_setting;
  574. unsigned long flags;
  575. timr = lock_timer(timer_id, &flags);
  576. if (!timr)
  577. return -EINVAL;
  578. CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
  579. unlock_timer(timr, flags);
  580. if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
  581. return -EFAULT;
  582. return 0;
  583. }
  584. /*
  585. * Get the number of overruns of a POSIX.1b interval timer. This is to
  586. * be the overrun of the timer last delivered. At the same time we are
  587. * accumulating overruns on the next timer. The overrun is frozen when
  588. * the signal is delivered, either at the notify time (if the info block
  589. * is not queued) or at the actual delivery time (as we are informed by
  590. * the call back to do_schedule_next_timer(). So all we need to do is
  591. * to pick up the frozen overrun.
  592. */
  593. asmlinkage long
  594. sys_timer_getoverrun(timer_t timer_id)
  595. {
  596. struct k_itimer *timr;
  597. int overrun;
  598. long flags;
  599. timr = lock_timer(timer_id, &flags);
  600. if (!timr)
  601. return -EINVAL;
  602. overrun = timr->it_overrun_last;
  603. unlock_timer(timr, flags);
  604. return overrun;
  605. }
  606. /* Set a POSIX.1b interval timer. */
  607. /* timr->it_lock is taken. */
  608. static int
  609. common_timer_set(struct k_itimer *timr, int flags,
  610. struct itimerspec *new_setting, struct itimerspec *old_setting)
  611. {
  612. struct hrtimer *timer = &timr->it.real.timer;
  613. if (old_setting)
  614. common_timer_get(timr, old_setting);
  615. /* disable the timer */
  616. timr->it.real.interval.tv64 = 0;
  617. /*
  618. * careful here. If smp we could be in the "fire" routine which will
  619. * be spinning as we hold the lock. But this is ONLY an SMP issue.
  620. */
  621. if (hrtimer_try_to_cancel(timer) < 0)
  622. return TIMER_RETRY;
  623. timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
  624. ~REQUEUE_PENDING;
  625. timr->it_overrun_last = 0;
  626. /* switch off the timer when it_value is zero */
  627. if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
  628. return 0;
  629. /* Posix madness. Only absolute CLOCK_REALTIME timers
  630. * are affected by clock sets. So we must reiniatilize
  631. * the timer.
  632. */
  633. if (timr->it_clock == CLOCK_REALTIME && (flags & TIMER_ABSTIME))
  634. hrtimer_rebase(timer, CLOCK_REALTIME);
  635. else
  636. hrtimer_rebase(timer, CLOCK_MONOTONIC);
  637. timer->expires = timespec_to_ktime(new_setting->it_value);
  638. /* Convert interval */
  639. timr->it.real.interval = timespec_to_ktime(new_setting->it_interval);
  640. /* SIGEV_NONE timers are not queued ! See common_timer_get */
  641. if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE))
  642. return 0;
  643. hrtimer_start(timer, timer->expires, (flags & TIMER_ABSTIME) ?
  644. HRTIMER_ABS : HRTIMER_REL);
  645. return 0;
  646. }
  647. /* Set a POSIX.1b interval timer */
  648. asmlinkage long
  649. sys_timer_settime(timer_t timer_id, int flags,
  650. const struct itimerspec __user *new_setting,
  651. struct itimerspec __user *old_setting)
  652. {
  653. struct k_itimer *timr;
  654. struct itimerspec new_spec, old_spec;
  655. int error = 0;
  656. long flag;
  657. struct itimerspec *rtn = old_setting ? &old_spec : NULL;
  658. if (!new_setting)
  659. return -EINVAL;
  660. if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
  661. return -EFAULT;
  662. if (!timespec_valid(&new_spec.it_interval) ||
  663. !timespec_valid(&new_spec.it_value))
  664. return -EINVAL;
  665. retry:
  666. timr = lock_timer(timer_id, &flag);
  667. if (!timr)
  668. return -EINVAL;
  669. error = CLOCK_DISPATCH(timr->it_clock, timer_set,
  670. (timr, flags, &new_spec, rtn));
  671. unlock_timer(timr, flag);
  672. if (error == TIMER_RETRY) {
  673. rtn = NULL; // We already got the old time...
  674. goto retry;
  675. }
  676. if (old_setting && !error &&
  677. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  678. error = -EFAULT;
  679. return error;
  680. }
  681. static inline int common_timer_del(struct k_itimer *timer)
  682. {
  683. timer->it.real.interval.tv64 = 0;
  684. if (hrtimer_try_to_cancel(&timer->it.real.timer) < 0)
  685. return TIMER_RETRY;
  686. return 0;
  687. }
  688. static inline int timer_delete_hook(struct k_itimer *timer)
  689. {
  690. return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
  691. }
  692. /* Delete a POSIX.1b interval timer. */
  693. asmlinkage long
  694. sys_timer_delete(timer_t timer_id)
  695. {
  696. struct k_itimer *timer;
  697. long flags;
  698. retry_delete:
  699. timer = lock_timer(timer_id, &flags);
  700. if (!timer)
  701. return -EINVAL;
  702. if (timer_delete_hook(timer) == TIMER_RETRY) {
  703. unlock_timer(timer, flags);
  704. goto retry_delete;
  705. }
  706. spin_lock(&current->sighand->siglock);
  707. list_del(&timer->list);
  708. spin_unlock(&current->sighand->siglock);
  709. /*
  710. * This keeps any tasks waiting on the spin lock from thinking
  711. * they got something (see the lock code above).
  712. */
  713. if (timer->it_process) {
  714. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  715. put_task_struct(timer->it_process);
  716. timer->it_process = NULL;
  717. }
  718. unlock_timer(timer, flags);
  719. release_posix_timer(timer, IT_ID_SET);
  720. return 0;
  721. }
  722. /*
  723. * return timer owned by the process, used by exit_itimers
  724. */
  725. static void itimer_delete(struct k_itimer *timer)
  726. {
  727. unsigned long flags;
  728. retry_delete:
  729. spin_lock_irqsave(&timer->it_lock, flags);
  730. if (timer_delete_hook(timer) == TIMER_RETRY) {
  731. unlock_timer(timer, flags);
  732. goto retry_delete;
  733. }
  734. list_del(&timer->list);
  735. /*
  736. * This keeps any tasks waiting on the spin lock from thinking
  737. * they got something (see the lock code above).
  738. */
  739. if (timer->it_process) {
  740. if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
  741. put_task_struct(timer->it_process);
  742. timer->it_process = NULL;
  743. }
  744. unlock_timer(timer, flags);
  745. release_posix_timer(timer, IT_ID_SET);
  746. }
  747. /*
  748. * This is called by do_exit or de_thread, only when there are no more
  749. * references to the shared signal_struct.
  750. */
  751. void exit_itimers(struct signal_struct *sig)
  752. {
  753. struct k_itimer *tmr;
  754. while (!list_empty(&sig->posix_timers)) {
  755. tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
  756. itimer_delete(tmr);
  757. }
  758. }
  759. /* Not available / possible... functions */
  760. int do_posix_clock_nosettime(const clockid_t clockid, struct timespec *tp)
  761. {
  762. return -EINVAL;
  763. }
  764. EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
  765. int do_posix_clock_notimer_create(struct k_itimer *timer)
  766. {
  767. return -EINVAL;
  768. }
  769. EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create);
  770. int do_posix_clock_nonanosleep(const clockid_t clock, int flags,
  771. struct timespec *t, struct timespec __user *r)
  772. {
  773. #ifndef ENOTSUP
  774. return -EOPNOTSUPP; /* aka ENOTSUP in userland for POSIX */
  775. #else /* parisc does define it separately. */
  776. return -ENOTSUP;
  777. #endif
  778. }
  779. EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
  780. asmlinkage long sys_clock_settime(const clockid_t which_clock,
  781. const struct timespec __user *tp)
  782. {
  783. struct timespec new_tp;
  784. if (invalid_clockid(which_clock))
  785. return -EINVAL;
  786. if (copy_from_user(&new_tp, tp, sizeof (*tp)))
  787. return -EFAULT;
  788. return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
  789. }
  790. asmlinkage long
  791. sys_clock_gettime(const clockid_t which_clock, struct timespec __user *tp)
  792. {
  793. struct timespec kernel_tp;
  794. int error;
  795. if (invalid_clockid(which_clock))
  796. return -EINVAL;
  797. error = CLOCK_DISPATCH(which_clock, clock_get,
  798. (which_clock, &kernel_tp));
  799. if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
  800. error = -EFAULT;
  801. return error;
  802. }
  803. asmlinkage long
  804. sys_clock_getres(const clockid_t which_clock, struct timespec __user *tp)
  805. {
  806. struct timespec rtn_tp;
  807. int error;
  808. if (invalid_clockid(which_clock))
  809. return -EINVAL;
  810. error = CLOCK_DISPATCH(which_clock, clock_getres,
  811. (which_clock, &rtn_tp));
  812. if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
  813. error = -EFAULT;
  814. }
  815. return error;
  816. }
  817. /*
  818. * nanosleep for monotonic and realtime clocks
  819. */
  820. static int common_nsleep(const clockid_t which_clock, int flags,
  821. struct timespec *tsave, struct timespec __user *rmtp)
  822. {
  823. int mode = flags & TIMER_ABSTIME ? HRTIMER_ABS : HRTIMER_REL;
  824. int clockid = which_clock;
  825. switch (which_clock) {
  826. case CLOCK_REALTIME:
  827. /* Posix madness. Only absolute timers on clock realtime
  828. are affected by clock set. */
  829. if (mode != HRTIMER_ABS)
  830. clockid = CLOCK_MONOTONIC;
  831. case CLOCK_MONOTONIC:
  832. break;
  833. default:
  834. return -EINVAL;
  835. }
  836. return hrtimer_nanosleep(tsave, rmtp, mode, clockid);
  837. }
  838. asmlinkage long
  839. sys_clock_nanosleep(const clockid_t which_clock, int flags,
  840. const struct timespec __user *rqtp,
  841. struct timespec __user *rmtp)
  842. {
  843. struct timespec t;
  844. if (invalid_clockid(which_clock))
  845. return -EINVAL;
  846. if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
  847. return -EFAULT;
  848. if (!timespec_valid(&t))
  849. return -EINVAL;
  850. return CLOCK_DISPATCH(which_clock, nsleep,
  851. (which_clock, flags, &t, rmtp));
  852. }